Skip to content

mypy not recognizing attribute of class created with setattr #5719

Closed
@arnavb

Description

@arnavb

Let's say I have the following code in example.py:

class Example:
    def __init__(self) -> None:
        setattr(self, 'name', 'value')
    
    def a_function(self) -> bool:
        return self.name == 'value'

e = Example()

print(e.a_function())

When I run mypy example.py, I get:

example.py:6: error: "Example" has no attribute "name"

Is this expected behavior?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions