Now python suggest for attribute possible in this way:
obj=exc_value.objtry:
try:
d=dir(obj)
exceptTypeError: # Attributes are unsortable, e.g. int and strd=list(obj.__class__.__dict__.keys()) +list(obj.__dict__.keys())
d=sorted([xforxindifisinstance(x, str)])
hide_underscored= (wrong_name[:1] !='_')
ifhide_underscoredandtbisnotNone:
whiletb.tb_nextisnotNone:
tb=tb.tb_nextframe=tb.tb_frameif'self'inframe.f_localsandframe.f_locals['self'] isobj:
hide_underscored=Falseifhide_underscored:
d= [xforxindifx[:1] !='_']
exceptException:
returnNone
However, the obj may has no attribute "__dict__" if it has the attribute "__slots__". So list(obj.__class__.__dict__.keys()) + list(obj.__dict__.keys()) is not safe. And the class must have the attribute "__name__" that the object of the class cannot get.
In fact, the object.__dir__ is safer, it can exactly find all of the attribute that can be get with object.__getattribute__. So I think that we can change the way by using this way.
changed the title [-]`traceback.py`: Suggestion for `AttributeError` suggestion mechanism[/-][+]Safer attribute suggestion for a slotted class with custom `__dir__`[/+]on Oct 11, 2025
then there won't be any completion suggestion because we're catching the AttributeError and returning None directly. I'm considering this as a bug because we're not trying hard enough here to get the attributes. OTOH, I need to check if object.__dir__ is safe to call (that is, if this wouldn't trigger side-effects).
changed the title [-]Safer attribute suggestion for a slotted class with custom `__dir__`[/-][+]Safer attribute suggestion for classes with custom "bad" `__dir__`[/+]on Oct 11, 2025
It's not just for this specific case. I need to know what happens in C for that. Now, I've confirmed that object.__dir__ is simply the default dir() so nothing fancy should happen. However, what is concerning is that if dir() is failing, then object.__dir__(x) has no meaning because a failing dir() means a custom dir(), which in turn means that the default one is not good.
Therefore, I think we should just give up on using __dict__ here and only rely on the class attributes.
It's not just for this specific case. I need to know what happens in C for that. Now, I've confirmed that object.__dir__ is simply the default dir() so nothing fancy should happen. However, what is concerning is that if dir() is failing, then object.__dir__(x) has no meaning because a failing dir() means a custom dir(), which in turn means that the default one is not good.
Therefore, I think we should just give up on using __dict__ here and only rely on the class attributes.
The object.__dir__ won't sort the result. This is the biggest difference.
It doesn't matter. As you can see, object.__dir__(a) is lacking "abc". In case object.__dir__(a) actually outputs something that was not meant to be returned by the custom __dir__, it will be worse. So relying on the default behavior of __dir__ is not really a better idea IMO. I prefer to have missing suggestions rather than having incorrect ones.
In fact, a.__dir__() is also unsorted. So, we can try to just call it. What will sort the result is only dir(), unless the code sorted the result before in __dir__
Activity
StanFromIreland commented on Oct 11, 2025
Please provide context, both in the title and issue body, I had to go grep the lines you provided.
Locked-chess-official commented on Oct 11, 2025
traceback.py at line 1635-1653
StanFromIreland commented on Oct 11, 2025
Looking at the discussion on the recent PR which added these lines, #131001, the current approach was preferred.
Locked-chess-official commented on Oct 11, 2025
Yes. However, object.__dir__ is unchangable and it won't sort the list, so it cannot raise TypeError:
And then d = sorted([x for x in d if isinstance(x, str)]) make sure that the 1 in the list will be ignore.
This way is more powerful.
picnixz commented on Oct 11, 2025
There is indeed an "issue":
If you have:
then there won't be any completion suggestion because we're catching the AttributeError and returning None directly. I'm considering this as a bug because we're not trying hard enough here to get the attributes. OTOH, I need to check if object.__dir__ is safe to call (that is, if this wouldn't trigger side-effects).
Locked-chess-official commented on Oct 11, 2025
It seems that it is normal with object.__dir__:
picnixz commented on Oct 11, 2025
It's not just for this specific case. I need to know what happens in C for that. Now, I've confirmed that object.__dir__ is simply the default dir() so nothing fancy should happen. However, what is concerning is that if dir() is failing, then object.__dir__(x) has no meaning because a failing dir() means a custom dir(), which in turn means that the default one is not good.
Therefore, I think we should just give up on using __dict__ here and only rely on the class attributes.
Locked-chess-official commented on Oct 11, 2025
And then I will test for this object:
Locked-chess-official commented on Oct 11, 2025
The object.__dir__ won't sort the result. This is the biggest difference.
picnixz commented on Oct 11, 2025
It doesn't matter. As you can see, object.__dir__(a) is lacking "abc". In case object.__dir__(a) actually outputs something that was not meant to be returned by the custom __dir__, it will be worse. So relying on the default behavior of __dir__ is not really a better idea IMO. I prefer to have missing suggestions rather than having incorrect ones.
Locked-chess-official commented on Oct 11, 2025
In fact, a.__dir__() is also unsorted. So, we can try to just call it. What will sort the result is only dir(), unless the code sorted the result before in __dir__
picnixz commented on Oct 11, 2025
Oh, I thought there was some callback for the __dir__ method. Ok, yes, let's just call __dir__. Thanks for that.
gh-139933: correctly suggest attributes for classes with a custom `__…
[3.14] pythongh-139933: correctly suggest attributes for classes with…
[3.14] gh-139933: correctly suggest attributes for classes with a cus…
[3.13] pythongh-139933: correctly suggest attributes for classes with…
[3.13] gh-139933: correctly suggest attributes for classes with a cus…
pythongh-139933: correctly suggest attributes for classes with a cust…