Skip to content

Class Decorators #1898

Answered by erictraut
hmc-cs-mdrissi asked this question in Q&A
May 22, 2021 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

Pyright doesn't support the use of dynamic arguments in a class declaration. Neither does mypy. Static type checkers need to understand a class's full inheritance hierarchy, so if you're dynamically generating that hierarchy, the type checker won't be able to reason about the resulting class.

If your intent is to return a class that is effectively the same as the class it is decorating (at least from an interface perspective), then you can annotate it as follows:

def make_cached_object(base_cls: Type[T]) -> Type[T]:
    class CachedObject(base_cls):  # type: ignore
        ....

    return cast(Type[T], CachedObject)

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@hmc-cs-mdrissi
Comment options

Answer selected by hmc-cs-mdrissi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants