Skip to content
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

Improve error message from os.fspath if __fspath__ is set to None #106046

Closed
AlexWaygood opened this issue Jun 23, 2023 · 2 comments · Fixed by #106082
Closed

Improve error message from os.fspath if __fspath__ is set to None #106046

AlexWaygood opened this issue Jun 23, 2023 · 2 comments · Fixed by #106082
Assignees
Labels
type-feature A feature request or enhancement

Comments

@AlexWaygood
Copy link
Member

AlexWaygood commented Jun 23, 2023

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

@barneygale
Copy link
Contributor

It also doesn't work for __bytes__():

>>> class C:
...   __bytes__ = None
... 
>>> bytes(C())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable

@AlexWaygood
Copy link
Member Author

It also doesn't work for __bytes__()

Yeah. I don't think there's any guarantee or promise that Python will do this for every magic method -- but if we have a use case here, then it could be worth it to implement it for __fspath__.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants