Feature or enhancement
A common Python idiom is to set a magic method to None 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 to None. For example:
>>> class Foo(list):
... __iter__ = None
...
>>> iter(Foo())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'Foo' object is not iterable
>>> class Bar(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 an os.PathLike sets __fspath__ to None, Python gives a bad error message when os.fspath is called on instances of that subclass:
>>> from pathlib import Path
>>> import os
>>> class Baz(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 to None the same way it handles magic methods such as __iter__ or __hash__ being set to None. The error message in such cases should be the expected str, bytes or os.PathLike object, not Baz error message that the interpreter gives when os.fspath is called on an object that has no __fspath__ method at all.
Linked PRs
Feature or enhancement
A common Python idiom is to set a magic method to
Nonein 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:However, Python doesn't currently do the same thing for
__fspath__. If a subclass of anos.PathLikesets__fspath__toNone, Python gives a bad error message whenos.fspathis called on instances of that subclass:Pitch
It would be nice if Python handled
__fspath__being set toNonethe 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 Bazerror message that the interpreter gives whenos.fspathis called on an object that has no__fspath__method at all.Linked PRs
os.fspathif__fspath__is set toNone#106082