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
When using __getattr__ at module level in order to perform lazy initializations of some attributes, pylance false reports these as not being able to import. At the same time, mypy does not seem to have any problem with this.
It should be noted that use of __getattr__ at module level is documented and approved via https://peps.python.org/pep-0562/ and working since python 3.7
Environment data
Pylance version: v2024.11.100 (pre-release)
OS and version: MacOS
Python version (& distribution if applicable, e.g. Anaconda): 3.12
Code Snippet
To reproduce the issue, one needs to attempt to import foo from some_module, with that module having a code similar to:
# some_module.pyfromtypingimportAnydef__getattr__(name: str) ->Any:
matchname:
case"foo":
return123msg=f"module {__name__!r} has no attribute {name!r}"raiseAttributeError(msg)
__all__= ("foo",)
Repro Steps
XXX
Perform the import succesfully.
XXX
XXX
Logs
XXX
The text was updated successfully, but these errors were encountered:
Thanks for the issue but this isn't a supported pattern for a static type checker. I presume in your real code you're doing something like this:
importimportlibdef__getattr__(name):
ifname=="output"returnimportlib.import_module(".somepath.output")
raiseAttributeError(f"module {__name__!r} has no attribute {name!r}")
The workaround for this situation is to have a type stub as described in this issue: microsoft/pyright#3989
When using
__getattr__
at module level in order to perform lazy initializations of some attributes, pylance false reports these as not being able to import. At the same time, mypy does not seem to have any problem with this.It should be noted that use of
__getattr__
at module level is documented and approved via https://peps.python.org/pep-0562/ and working since python 3.7Environment data
Code Snippet
To reproduce the issue, one needs to attempt to
import foo from some_module
, with that module having a code similar to:Repro Steps
Perform the import succesfully.
XXX
XXX
Logs
The text was updated successfully, but these errors were encountered: