Skip to content

Inner/Nested Class Within Function Return Type Not Defined #1834

Answered by erictraut
jodhus asked this question in Q&A
Discussion options

You must be logged in to vote

Mypy is correct to generate an error here. Pylint is probably just missing a check.

The InnerClass symbol is defined in an inner scope and has no meaning where you are referencing it. The quotes mean that forward references are allowed, but this symbol is not a forward reference.

If you want the type declaration to be visible outside of the function, you should declare it outside of the function, like this:

class MyClass:
    pass

def class_factory() -> type[MyClass]:
    return MyClass

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@jodhus
Comment options

Answer selected by jodhus
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