-
Apologies for the title word salad. I have a static class factory function that I defined as returning the inner class; however, I'm getting mixed results with type checking: def class_factory() -> "InnerClass":
class InnerClass:
pass
return InnerClass Pylint seems to have no problem, but Mypy is complaining that "InnerClass" is not defined. I've tried Is there a better way to define this or is this potentially a bug in Mypy? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Mypy is correct to generate an error here. Pylint is probably just missing a check. The 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 |
Beta Was this translation helpful? Give feedback.
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: