-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
gh-139933: correctly suggest attributes for classes with a custom __dir__
#139950
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
base: main
Are you sure you want to change the base?
Conversation
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
860ab2d
Compare
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
2478e05
Compare
|
Using a |
|
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 |
|
Or did I misunderstand and you want to test that there is indeed nothing good here? |
|
Note: the exception would occur in |
If the modulename in |
|
I don't understand. Your example already fails at the import sys
sys.modules["foo"] = int
from foo import reaThis 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: |
|
That's because you're importing EDIT: when you write For instance: class FakeMeta(type):
__name__ = "re"
class FakeName(metaclass=FakeMeta):
pass
import sys
sys.modules["fake"] = FakeName
try:
from fake import escape
except ImportError as exc:
print(exc.name) # prints "re"
print(exc.name_from) # prints "escape"IOW, when writing |
|
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 |
|
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 |
|
I think I understand what you mean. But since |
2478e05 to
5dfb399
Compare
|
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
652970c
Compare


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