Skip to content

Fix #11971 #11972

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 15, 2022
Merged

Fix #11971 #11972

merged 3 commits into from
Jan 15, 2022

Conversation

mrossinek
Copy link
Contributor

Description

Allows PlaceholderNode objects before raising an error for the attempted
reuse of a member name inside an Enum.

Test Plan

As explained in #11971 I (unfortunately) was not yet able to find a minimal reproducing example for this.
I can verify that this fixes my scenario via which I encountered the bug, but this is obviously not sufficient.

If you have any ideas how we can reproduce this, I am happy to add a corresponding unittest.

Allows PlaceholderNode objects before raising an error for the attempted
reuse of a member name inside an Enum.
@97littleleaf11
Copy link
Collaborator

basically looks good to me. cc @sobolevn

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

Please, add a test case (I really a fan of adding all regressions as test cases).

I found this minimal repro:

from enum import Enum
from typing import Callable, Dict


class Event(Enum):
    PreAddCommand: Event = Callable[[str], None]
    PostAddCommand: Event = Callable[[Dict[str, "Missing"]], None]

Note that Missing is not defined on purpose.
Looks fine to you? 🙂

@mrossinek
Copy link
Contributor Author

Interesting, thanks a lot @sobolevn!

So is the failure I was observing related to the use of a forward reference? If so, what makes the following two cases different?

PostAddCommand: Event = Callable[[Dict[str, "Entry"]], None]
PostEditCommand: Event = Callable[["Entry"], None]

The PostEditCommand did not raise an error hence I didn't suspect the forward reference to be the culprit 🤔

from typing import Callable, Dict

class Foo(Enum):
Bar: Foo = Callable[[str], None] # E: Value of type "int" is not indexable
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't quite understand this error which mypy is raising regarding int not being indexable. How is this relevant here?
Or is this due to some default of Enum assuming its values are of type int? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For completeness, this is the output of this test failing on master @ 6c1eb5b:

E   AssertionError: Unexpected type checker output (/home/max/Git/mypy/test-data/unit/check-enum.test, line 1460)
------------------------------------------------- Captured stderr call -------------------------------------------------
Expected:
  main:6: error: Value of type "int" is not indexable
  main:7: error: Value of type "int" is not indexable (diff)
  main:7: error: Name "Missing" is not defined  (diff)
Actual:
  main:6: error: Value of type "int" is not indexable
  main:7: error: Attempted to reuse member name "Baz" in Enum definition "Foo" (diff)
  main:7: error: Value of type "int" is not indexable (diff)
  main:7: error: Name "Missing" is not defined  (diff)

Alignment of first line difference:
  E: main:7: error: Value of type "int" is not indexable...
  A: main:7: error: Attempted to reuse member name "Baz" in Enum definition "...
                    ^

Copy link
Member

@sobolevn sobolevn Jan 13, 2022

Choose a reason for hiding this comment

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

Callable is defined as Callable = 0 in our test fixtures. You can define your own type instead for tests:

class Some(Generic[T]): ...

It should not really matter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright then I assume it is fine as is once the CI passes. If not, please let me know

mrossinek added a commit to mrossinek/cobib that referenced this pull request Jan 13, 2022
Revert this once python/mypy#11972 is merged and
released.
@mrossinek mrossinek requested a review from sobolevn January 15, 2022 10:37
Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

Looks like it fixes this cryptic error message.

But, I would improve a test case a bit.
Maybe it will be a good idea to move this case into pythoneval.test? This way we will remove # E: Value of type "int" is not indexable error message, because it will use real typing.py

@mrossinek
Copy link
Contributor Author

Alright I moved the new test as you suggested. I tried removing the Incompatible types error but when removing the : Foo type declaration the error which I am trying to fix here does not occur on master.
Here is the error which this test produces on current master (17850b3):

Expected:
  _testEnumValueWithPlaceholderNodeType.py:5: error: Incompatible types in as...
  _testEnumValueWithPlaceholderNodeType.py:6: error: Incompatible types in assignment (expression has type "object", variable has type "Foo") (diff)
  _testEnumValueWithPlaceholderNodeType.py:6: error: Name "Missing" is not defined (diff)
Actual:
  _testEnumValueWithPlaceholderNodeType.py:5: error: Incompatible types in as...
  _testEnumValueWithPlaceholderNodeType.py:6: error: Attempted to reuse member name "Baz" in Enum definition "Foo" (diff)
  _testEnumValueWithPlaceholderNodeType.py:6: error: Incompatible types in assignment (expression has type "object", variable has type "Foo") (diff)
  _testEnumValueWithPlaceholderNodeType.py:6: error: Name "Missing" is not defined (diff)

Alignment of first line difference:
  E: ...odeType.py:6: error: Incompatible types in assignment (expression has...
  A: ...odeType.py:6: error: Attempted to reuse member name "Baz" in Enum def...
                             ^

@mrossinek mrossinek requested a review from sobolevn January 15, 2022 14:43
Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

Awesome, thank you. I will approve it and leave it for someone else to double check and merge.

@97littleleaf11 97littleleaf11 merged commit 2bfa3b4 into python:master Jan 15, 2022
@mrossinek mrossinek deleted the fix-11971 branch January 15, 2022 20:58
tushar-deepsource pushed a commit to DeepSourceCorp/mypy that referenced this pull request Jan 20, 2022
Allows PlaceholderNode objects before raising an error for the attempted
reuse of a member name inside an Enum.
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.

3 participants