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
{{ message }}
Repository navigation
Safer attribute suggestion for classes with custom "bad" __dir__ #139933
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
[-]Use safer way "object.__dir__"[/-][+]`traceback.py`: Suggestion for `AttributeError` suggestion mechanism[/+]StanFromIreland commentedon 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 commentedon Oct 11, 2025
traceback.py at line 1635-1653
StanFromIreland commentedon Oct 11, 2025
Looking at the discussion on the recent PR which added these lines, #131001, the current approach was preferred.
Locked-chess-official commentedon 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.
[-]`traceback.py`: Suggestion for `AttributeError` suggestion mechanism[/-][+]Safer attribute suggestion for a slotted class with custom `__dir__`[/+]picnixz commentedon 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).[-]Safer attribute suggestion for a slotted class with custom `__dir__`[/-][+]Safer attribute suggestion for classes with custom "bad" `__dir__`[/+]Locked-chess-official commentedon Oct 11, 2025
It seems that it is normal with
object.__dir__:picnixz commentedon 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 defaultdir()so nothing fancy should happen. However, what is concerning is that ifdir()is failing, thenobject.__dir__(x)has no meaning because a failingdir()means a customdir(), 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 commentedon Oct 11, 2025
And then I will test for this object:
Locked-chess-official commentedon Oct 11, 2025
The
object.__dir__won't sort the result. This is the biggest difference.picnixz commentedon Oct 11, 2025
It doesn't matter. As you can see,
object.__dir__(a)is lacking "abc". In caseobject.__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 commentedon Oct 11, 2025
In fact,
a.__dir__()is also unsorted. So, we can try to just call it. What will sort the result is onlydir(), unless the code sorted the result before in__dir__picnixz commentedon 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…