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

Error on instance property and init-only variable with the same name in a dataclass #12046

Open
mbraakhekke opened this issue Jan 23, 2022 · 5 comments · May be fixed by #14299 or #17219
Open

Error on instance property and init-only variable with the same name in a dataclass #12046

mbraakhekke opened this issue Jan 23, 2022 · 5 comments · May be fixed by #14299 or #17219
Labels
bug mypy got something wrong topic-dataclasses topic-runtime-semantics mypy doesn't model runtime semantics correctly

Comments

@mbraakhekke
Copy link

Bug Report

Consider the following class definition:

from dataclasses import dataclass, InitVar
@dataclass
class MyClass:
    prop: InitVar[int]
    _prop: int

    def __post_init__(self, prop: int):
        self._prop = prop

    @property
    def prop(self) -> int:
        return self._prop

Mypy reports an error on line 10: Name "prop" already defined on line 4.

Is this a false positive? If not, how can I avoid this short of renaming the "prop" attribute or getter method (or using #typing: ignore)?

Environment

  • Mypy version used: 0.931
  • Python version used: 3.10.1
  • Operating system and version: Windows 10 x64
@mbraakhekke mbraakhekke added the bug mypy got something wrong label Jan 23, 2022
@A5rocks
Copy link
Contributor

A5rocks commented Jan 23, 2022

This is a problem with mypy's dataclasses plugin, I suppose. However, as mypy would have already processed the class (I think?) before the plugin gets to it, I'm not certain it can be fixed. I suppose the workaround would be to # type: ignore the necessary lines and run mypy with --warn-unused-ignores (you can stick this in a config file too) to tell you when you can remove the # type: ignore, ie because mypy gains support.

@mbraakhekke
Copy link
Author

Thanks. I'll implement the suggest workaround.

@joshuafishman
Copy link

I also get "MyClass" has no attribute "prop" [attr-defined] in mypy for any reference of that attribute -- the workaround suggested doesn't address these.

@joshuafishman
Copy link

e.g.

from dataclasses import dataclass, InitVar


@dataclass
class Baz:
    foo: InitVar[int]

    @property  # type: ignore
    def foo(self) -> int:
        return 0


print(Baz(0).foo)

results in error: "Baz" has no attribute "foo" [attr-defined]

@ikonst
Copy link
Contributor

ikonst commented Mar 9, 2023

Maybe if TypeInfo would start having a separate dict for annotations rather than put it all into names. Might be a bit of a refactor though, which could be less painful if TypeInfo.names becomes a "wrapper" around two maps, one with annotations and the other with identifiers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-dataclasses topic-runtime-semantics mypy doesn't model runtime semantics correctly
Projects
None yet
5 participants