-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pyright doesn't respect the documented resolution order #9752
Comments
For the sake of completeness — after removing import typing
if typing.TYPE_CHECKING:
...
else:
... This proves that it can find the stdlib I attempted to create a config file in the hope of teaching pyright where the root path was, so that it would stop the incorrect import, but I could never change the outcome. |
Pyright is working as intended here, so I don't consider this a bug. When you use a relative import statement (e.g. You can see this if you add print statements to your code: import typing
print(typing)
from .typing import Data
print(typing) Now create a dummy file called <module 'typing' from '/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/typing.py'>
<module 'mymodule.typing' from '<redacted>/mymodule/typing.py'> |
Ah, yes, now it's obvious to me why FYI, in my real code, the relative import comes after accessing import typing
if typing.TYPE_CHECKING:
from .typing import Data
else:
... # lazy import of Data To sum up, while the following code works, import typing
print(typing.TYPE_CHECKING)
from .typing import Data
print(typing.Data) Raising the error message at the level of Note that pyright will happily accept the following code, even though it crashes with an AttributeError: import typing
print(typing.Data)
from .typing import Data Ideally, there would be a correlation between pyright declaring Depending on how much you care about UX, you may want to make pyright reject that last example. I would find it intuitive to raise an error at the level of |
PS: I understand that you're not going to change your mind on the design of pyright. I will alias an import to avoid shadowing. If the above gives you an idea to improve the UX, fantastic; if not, thank you for your fast response — my problem is solved :-) |
… shadow the same symbol and an attribute is accessed between the two import statements. This addresses #9752.
… shadow the same symbol and an attribute is accessed between the two import statements. This addresses #9752.
… shadow the same symbol and an attribute is accessed between the two import statements. This addresses #9752.
Yes, the expression |
Describe the bug
Here are minimal reproduction instructions for an issue that affects https://github.com/python-websockets/websockets.
mymodule/__init__.py
mymodule/typing.py
Expected behavior
No error is raised. The code is valid.
Actual behavior
pyright raises the error shown above.
Analysis
pyright believes incorrectly that
typing
ismyproject.typing
rather than the stdlib'styping
module.Indeed, adding
reveal_type(typing.Data)
in__init__.py
gives the following incorrect result:Based on the import resolution docs, I expect pyright to find the stdlib module at step 4. However, it appears to continue until step 6.
I understand why step 6 exists as a last resort — it mimicks Python adding the directory containing a script to the import path. I don't understand why it takes precedence over step 4.
I checked the search paths in the verbose output and confirmed that there's a
typing.pyi
stub defining theTYPE_CHECKING
attribute in the first path of the list. This should match at step 4.I looked for duplicate issues and found #7226. However, my issue is different. I can live with step 6 of the import resolution happening. I just need step 4 to take precedence over step 6, which appears to be your intent, but isn't the result I'm getting with this very basic example.
The text was updated successfully, but these errors were encountered: