[Python] Subclassing typing.Any

Discussão em 'Python' iniciado por Stack, Julho 7, 2025.

  1. Stack

    Stack Membro Participativo

    The documentation for typing.Any says


    Changed in version 3.11: Any can now be used as a base class. This can be useful for avoiding type checker errors with classes that can duck type anywhere or are highly dynamic.
    ​

    What kind of errors is this trying to avoid? At first, I thought the situation in mind was

    class Foo:
    def __getattr__(self, attr: str) -> typing.Any:
    if attr == 'bar':
    return 5
    raise AttributeError(attr)

    def print_bar(foo: Foo) -> None:
    print(foo.bar)


    However, mypy 0.971 with Python 3.10.6 doesn't produce any errors.

    Continue reading...

Compartilhe esta Página