FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Properly account for `member` and `nonmember` in `TypeInfo.enum_members` by sobolevn · Pull Request #18559 · python/mypy · GitHub

Repository navigation

Properly account for member and nonmember in TypeInfo.enum_members - #18559

Merged
sobolevn merged 5 commits into
masterfrom
issue-18557
Feb 14, 2025
Merged

sobolevn merged 5 commits into
masterfrom
issue-18557

Conversation

Copy link
Copy Markdown
Member

Closes #18557

This comment has been minimized.

tyralla left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This is the first time I heard about member and nomember, but I appreciate the opportunity to learn...

I played a little and found your fix does not cover the following slight change of your example:

from enum import Enum, member
from typing import reveal_type

class Pet(Enum):
    CAT = 1

    @member
    def human(self) -> int:
        return 2

def a(x: Pet) -> None:
    if x is not Pet.human:  # `human` instead of `CAT`, as in the original example
        reveal_type(x)  # N: Revealed type is "__main__.Pet"

Expected would be Literal[__main__.Pet.CAT]

However, this does not seem to be related to TypeInfo.enum_members, so this comment is just a suggestion for an extension of this or an independent PR.

Because of my lack of experience, I am not really sure if some edge cases are missed, but to me, this change looks good and is definitely an improvement.

sobolevn commented Jan 29, 2025 •
edited
Loading

Copy link
Copy Markdown
Member Author

However, this does not seem to be related to TypeInfo.enum_members, so this comment is just a suggestion for an extension of this or an independent PR.

Yeah, there are multiple other problems with Enum. For example: #18563

I think that the example that you've mentioned is happening because right now Enum cannot properly unwrap callable types or something.

Comment thread mypy/nodes.py
(
isinstance(sym.node, Var)
and name not in EXCLUDED_ENUM_ATTRIBUTES
and not name.startswith("__")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Should this condition exist for enum.member too because #18563?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Hm, good question. Let's keep it for now, I will change it in #18563 if needed.

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

freqtrade (https://github.com/freqtrade/freqtrade): 1.98x slower (128.9s -> 255.0s in a single noisy sample)

Copy link
Copy Markdown
Member Author

My further plan: get rid of is_final_enum_value and use enum_members everywhere, PR is incoming!

sobolevn merged commit f404b16 into master Feb 14, 2025
sobolevn deleted the issue-18557 branch February 14, 2025 07:18

Copy link
Copy Markdown
Member Author

Done: #18675

x612skm pushed a commit to x612skm/mypy-dev that referenced this pull request Feb 24, 2025
…rs` (python#18559)

Closes python#18557

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.enum_members property does not account to @member decorator and call

3 participants


Back | FazBrowse Home | New Git URL