Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
Description
Feature or enhancement
A common Python idiom is to set a magic method toNone in a subclass if you want to disable the behaviour that the magic method enables. If you do so, a nice error message will be given if a user tries to "call" the magic method that's been set toNone. For example:
>>>classFoo(list):...__iter__=None...>>>iter(Foo())Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: 'Foo' object is not iterable>>>classBar(int):...__hash__=None...>>>hash(Bar(42))Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: unhashable type: 'Bar'
However, Python doesn't currently do the same thing for__fspath__. If a subclass of anos.PathLike sets__fspath__ toNone, Python gives a bad error message whenos.fspath is called on instances of that subclass:
>>>from pathlibimport Path>>>import os>>>classBaz(Path):...__fspath__=None...>>> os.fspath(Baz())Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: 'NoneType' object is not callable
Pitch
It would be nice if Python handled__fspath__ being set toNone the same way it handles magic methods such as__iter__ or__hash__ being set toNone. The error message in such cases should be theexpected str, bytes or os.PathLike object, not Baz error message that the interpreter gives whenos.fspath is called on an object that has no__fspath__ method at all.