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

False negative when accessing annotated class attributes #13103

Closed
denballakh opened this issue Jul 11, 2022 · 5 comments · Fixed by #14125
Closed

False negative when accessing annotated class attributes #13103

denballakh opened this issue Jul 11, 2022 · 5 comments · Fixed by #14125
Labels
bug mypy got something wrong good-first-issue topic-descriptors Properties, class vs. instance attributes topic-runtime-semantics mypy doesn't model runtime semantics correctly topic-variable-scope

Comments

@denballakh
Copy link
Contributor

denballakh commented Jul 11, 2022

Consider this example:

class X:
    __slots__ = ('a',)
    a: int

print(X.a) # <member 'a' of 'X' objects>
reveal_type(X.a) # builtins.int
reveal_type(X.a.__get__) # Any
    # "int" has no attribute "__get__"; maybe "__gt__", "__ge__", or "__ne__"?

I expect to get member_descriptor and some Callable, but instead i see two wrong reveals and one error.

I can use this bug to write unsafe code:

class X:
    __slots__ = ('a',)
    a: int

x = X()
x.a = X.a # no error!
assert isinstance(x.a, int), x.a
# AssertionError: <member 'a' of 'X' objects>

My Environment

  • mypy 0.961 (compiled: no)
  • no command-line flags, no config file
  • CPython 3.10.4
  • Windows 10
@denballakh denballakh added the bug mypy got something wrong label Jul 11, 2022
@AlexWaygood AlexWaygood added topic-runtime-semantics mypy doesn't model runtime semantics correctly topic-descriptors Properties, class vs. instance attributes topic-variable-scope labels Jul 11, 2022
@JukkaL
Copy link
Collaborator

JukkaL commented Jul 16, 2022

Some hints:

  • The slots of a class are available in the slots attribute of TypeInfo
  • The relevant code is in mypy.checkmember. Probably look at analyze_type_callable_member_access and analyze_type_type_member_access.
  • We can probably just generate an error if trying to access Cls.x if x has a slot. It's rarely the right thing to do.

@denballakh
Copy link
Contributor Author

denballakh commented Jul 17, 2022

This might be related:

class X:
    __slots__ = ('x',)
    x: int = 1

# Traceback (most recent call last):
#   File "...", line ..., in <module>
#     class X:
# ValueError: 'x' in __slots__ conflicts with class variable

reveal_type(X.x) # int
reveal_type(X().x) # int

Mypy gives no errors, pylint complains Value 'x' in slots conflicts with class variable (class-variable-slots-conflict).
At runtime i have ValueError: 'x' in __slots__ conflicts with class variable

@DanielGoman
Copy link

Hello :)
I'm new to open source contribution.
If this issue is still relevant, I'd like to start working on it.
Could you please assign it to me?

@hmcty
Copy link
Contributor

hmcty commented Nov 18, 2022

My first go at contributing, let me know if you had something else in mind @JukkaL

@hmcty
Copy link
Contributor

hmcty commented Nov 30, 2022

Any updates on this?

hauntsaninja pushed a commit that referenced this issue Dec 13, 2022
Fixed #13103  

Adds a check to class attribute access to ensure it isn't a defined
slot.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong good-first-issue topic-descriptors Properties, class vs. instance attributes topic-runtime-semantics mypy doesn't model runtime semantics correctly topic-variable-scope
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants