Skip to content
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

Produce error when assigning Enum or TypedDict as attribute #8107

Merged
merged 1 commit into from Dec 12, 2019
Merged

Produce error when assigning Enum or TypedDict as attribute #8107

merged 1 commit into from Dec 12, 2019

Conversation

TH3CHARLie
Copy link
Collaborator

@TH3CHARLie TH3CHARLie commented Dec 8, 2019

resolves #8009, simply following solution by #7662

Additionally, when I was finishing the first version of the PR I noticed that some of #7662 could be refined:

mypy/mypy/semanal.py

Lines 2152 to 2154 in e8ae122

if isinstance(s.lvalues[0], MemberExpr):
self.fail("NamedTuple type as an attribute is not supported", lvalue)
return False

Here using lvalue instead of s.lvalues[0] would be more concise.
[case testNamedTupleAtRunTime]
from typing import NamedTuple
class A:
def __init__(self) -> None:
self.b = NamedTuple('x', [('s', str), ('n', int)])
reveal_type(A().b)
[out]
_testNamedTupleAtRunTime.py:5: error: NamedTuple type as an attribute is not supported
_testNamedTupleAtRunTime.py:7: note: Revealed type is 'Any'

The test is in pythoneval.test, but it makes more sense in check-namedtuple.test and the name could be also changed accordingly.

@ilevkivskyi What's your opinion on this? Should these changes be made? And if so, should they be made in this PR or a sperate one.

@ilevkivskyi
Copy link
Member

Should these changes be made? And if so, should they be made in this PR or a sperate one.

Yes, I think in a separate PR.

ilevkivskyi pushed a commit that referenced this pull request Dec 9, 2019
…me for variable (#8112)

Followed the discussion in #8107 (comment), refine the code merged in #7662 by:

- move testcase from `pythoneval.test` to `check-namedtuple.test` and rename it from `testNamedTupleAtRunTime` to `testAssignNamedTupleAsAttribute`
- change `s.lvalues[0]` to `lvalue` to make it more concise
Copy link
Collaborator

@msullivan msullivan left a comment

Choose a reason for hiding this comment

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

This seems reasonable. Thanks!

@msullivan msullivan merged commit a2ab97b into python:master Dec 12, 2019
@TH3CHARLie TH3CHARLie deleted the error-when-enum-or-typeddict-as-attr branch December 12, 2019 02:00
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" has no attribute "__iter__" on instance attributes defined with functional API
3 participants