Skip to content

Support __getattr__ #520

Closed
Closed

Description

@JukkaL

Support overloading attribute access by defining a __getattr__ method. If a class has a __getattr__ method, mypy should let programs access any attributes, and the type would be the return type of __getattr__. Example:

class A:
    def __getattr__(self, name: str) -> int: return 2
a = A()
print(a.foobar)   # okay, type is int

Activity

  1. tommyjcarpenter commented on Dec 24, 2021

    @tommyjcarpenter

    I hit a similar issue today; I have found I need to change my code from:

    table: str = getattr(someobj, "someprop", "some_constant")
    

    to

    table: str = getattr(someobj, "someprop") or "some_constant"
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions