Decorators over __init__()
make classes unconstructable until after the class is declared, even within functions
#17021
Labels
bug
mypy got something wrong
Bug Report
It should be possible, within a function, to construct an instance of a class which is declared globally later in the module. Applying a decorator factory to a class's
__init__()
causes constructed instances to have typeAny
, but only before the class declaration1. (A plain decorator doesn't trigger this issue.)This probably explains #14519, and may be related to #11293.
To Reproduce
[mypy-play.net]
Expected Behavior
The tool should issue no error for
function_pre()
; it's just as valid asfunction_post()
.Actual Behavior
Any construction of
SomeClass
before that type is declared (i.e., before the end of its body) has typeAny
instead ofSomeClass
.Your Environment
mypy.ini
(and other config files): none necessary[Edited for clarity, and to simplify the reproduction case by removing vestigial code.]
Footnotes
some functions within the class body are affected as well; I haven't narrowed down the exact point constructing the class starts to behave normally, but I can do upon request. ↩
The text was updated successfully, but these errors were encountered: