You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Lazy imports break dataclass InitVars in extremely rare scenarios #160665
fromdataclassesimportInitVar, dataclasslazyfromtesting_2importA@dataclassclassB:
a: InitVar[A]
def__post_init__(self, a: A) ->None:
print(a)
If you run testing.py with python 3.15 you will get TypeError: B.__post_init__() missing 1 required positional argument: 'a'
The key thing that causes the error seems to be the presence of the import cycle combined with the lazy import. It's worth noting that if you use a TYPE_CHECKING guard instead as described below, it does also work on 3.15.
I found this issue while migrating a bunch of my TYPE_CHECKING guards to lazy imports - infact as PEP 810 says "Lazy imports eliminate the common need for TYPE_CHECKING guards". The version of python that breaks is cpython-3.15.0a7-linux-x86_64-gnu. The code instead works on cpython-3.14.3-linux-x86_64-gnu, if of course you replace the lazy import in testing_3.py with a if TYPE_CHECKING: block
Activity