Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Allow conversion of number to enum type when underlying type is assignable#1179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
slide wants to merge2 commits intopythonnet:masterfromslide:enum_conversion
Closed

Conversation

slide
Copy link
Contributor

@slideslide commentedJul 6, 2020
edited
Loading

What does this implement/fix? Explain your changes.

In .NET you can do the following:

enum FooEnum {    One = 1,    Three = 3,    Four = 4,}FooEnum x = (FooEnum)2;

This patch allows this from Python code as well.

Does this close any currently open issues?

Possibly#935

Any other comments?

I would definitely like eyes on this, I am possibly missing something :-)

Checklist

Check all those that are applicable and complete.

  • Make sure to include one or more tests for your change
  • If an enhancement PR, please create docs and at best an example
  • Add yourself toAUTHORS
  • Updated theCHANGELOG

In .NET you can do the following:```enum FooEnum {One = 1,Three = 3,Four = 4,}FooEnum x = (FooEnum)2;```This patch allows this from Python code as well.
@lostmsu
Copy link
Member

@slide in .NET you can't just assign a numeric value to an enum. You have to make an explicit cast for a good reason. The most pythonic way I see this could work isFooEnum(42). Implicit conversion is a bad idea.

@slide
Copy link
ContributorAuthor

That's true, it does require the typecast. This only allows assignment/coercion when the underlying type is assignable, is that still too lenient?

@lostmsu
Copy link
Member

lostmsu commentedJul 6, 2020
edited
Loading

@slide oh, I checked the code ofToEnum and it seems problematic on its own. I think what you will find is thatIsAssignableFrom will succeed in most cases.

I actually think Python.NET should have required an explicit cast from the beginning, and should not have converted enum values to Pythonint, which we have an opportunity to fix given 3.0 is a major version bump. I'd definitely take a PR, that would make that change.

Summoning@filmor into conversation.

@slide
Copy link
ContributorAuthor

Ok, thanks, I'll see if I can work on something like that.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@slide@lostmsu

[8]ページ先頭

©2009-2025 Movatter.jp