Uh oh!
There was an error while loading. Please reload this page.
gh-139933: correctly suggest attributes for classes with a custom __dir__ - #139950
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
a3477e3 to
860ab2dCompare
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
860ab2d to
2478e05Comparepicnixz
commented
Oct 11, 2025
Using a |
Locked-chess-official
commented
Oct 11, 2025
Add this test: The name is pending. |
I wouldn't count this as a legitimate use. Imports should only return modules. I don't want to support a bad |
picnixz
commented
Oct 11, 2025
Or did I misunderstand and you want to test that there is indeed nothing good here? |
picnixz
commented
Oct 11, 2025
Note: the exception would occur in |
If the modulename in |
I don't understand. Your example already fails at the importsyssys.modules["foo"] =intfromfooimportreaThis will raise an ImportError and we won't have a suggestion here at all. For the runtime completion suggestions, this is something different. The ImportError is: |
Locked-chess-official
commented
Oct 11, 2025
That's because you're importing EDIT: when you write For instance: classFakeMeta(type):
__name__="re"classFakeName(metaclass=FakeMeta):
passimportsyssys.modules["fake"] =FakeNametry:
fromfakeimportescapeexceptImportErrorasexc:
print(exc.name) # prints "re"print(exc.name_from) # prints "escape"IOW, when writing |
Locked-chess-official
commented
Oct 11, 2025
picnixz
commented
Oct 11, 2025
Ok, so this happens when we reuse the same name. But I don't think it's ok to mess things like that. My point is: what do you want me to do here? the behavior is somehow correct but if we want to have the same behavior for |
Locked-chess-official
commented
Oct 11, 2025
It is a behavior change. I think that it is needed to be recorded. |
(I deleted a duplicated comment). What do you mean it's a behavior change? what is changing from before? I did document the change but I will not document a change affecting an incorrect usage of |
picnixz
commented
Oct 11, 2025
I think I understand what you mean. But since |
2478e05 to
5dfb399Comparepicnixz
commented
Oct 11, 2025
Ok, I finally understood what you wanted to tell me. Indeed, with my PR, if by chance we store some weird object when importing, then we would indeed have a change of behavior. Now, the existing behavior in 3.13 and 3.14 is "working" but I would say that we're in the garbage-in garbage-out situation where, by chance, it also works. I don't know if it's preferrable to keep the behavior or actually fix it. Because I don't think it's actually correct to suggest something when the module to import is a class and not a module. OTOH, modules can be simply considered as special namespaces and someone could use classes to emulate singletons as we have for modules (something that is a namespace-like with some internal state emulated as class variables). Anyway, for now I've kept the existing behavior although I think it's partially incorrect. |
5dfb399 to
652970cCompareUh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, @picnixz and @ambv, I could not cleanly backport this to |
Sorry, @picnixz and @ambv, I could not cleanly backport this to |
ambv
commented
Mar 11, 2026
I'll deal with the backports, we gotta remove lazy import handling. |
GH-145827 is a backport of this pull request to the 3.14 branch. |
… a custom `__dir__` (pythonGH-139950) (cherry picked from commit 4722202) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Łukasz Langa <lukasz@langa.pl>
picnixz
commented
Mar 11, 2026
Thanks for the review & merge! |
… a custom `__dir__` (pythonGH-139950) (pythonGH-145827) (cherry picked from commit 4722202) (cherry picked from commit 0a80015) Co-authored-by: Łukasz Langa <lukasz@langa.pl> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
…om `__dir__` (pythonGH-139950) Co-authored-by: Łukasz Langa <lukasz@langa.pl>


This is based on the observation by @Locked-chess-official about
obj.__dir__returning the unsorted and unfiltered list of names that could be returned bydir. I misassumed thatobj.__dir__had some magic applied onto it so thanks for the idea.cc @ambv@devdanzin
__dir__#139933