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

[attr-defined] for attribute defined in __new__ #15806

Closed
Hocnonsense opened this issue Aug 3, 2023 · 2 comments
Closed

[attr-defined] for attribute defined in __new__ #15806

Hocnonsense opened this issue Aug 3, 2023 · 2 comments
Labels
bug mypy got something wrong

Comments

@Hocnonsense
Copy link

Hocnonsense commented Aug 3, 2023

Bug Report

This bug caused by defind an attribute in new for a class. This bug had been reported in #982.

To Reproduce

class MyObject:
    """
    A file that is either input or output of a rule.
    """

    __slots__ = ["a"]

    def __new__(cls, text):
        obj = object.__new__(cls)
        obj.a = text

        return obj


mo = MyObject("")
mo.a

Expected Behavior

Success: no issues found in 1 source file

Actual Behavior

2023/08/03.py:16: error: "MyObject" has no attribute "a"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: mypy 1.4.1 (compiled: yes)
  • Mypy command-line flags: mypy 2023/08/03.py
  • Mypy configuration options from mypy.ini (and other config files): None (default)
  • Python version used: Python 3.10.8
@Hocnonsense Hocnonsense added the bug mypy got something wrong label Aug 3, 2023
@SanjayManjunath
Copy link

Hey @Hocnonsense, I found out that 'a' should be initialized in the 'init' method since it's an instance attribute. Can you take a look at this and let me know if it's alright? https://mypy-play.net/?mypy=latest&python=3.10&gist=3e32e9112f3e5425c6aad2f8330e7ef8

@erictraut
Copy link

I think this is a duplicate of #1021.

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Aug 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

4 participants