Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Improve error message fromos.fspath if__fspath__ is set toNone #106046

Closed
Assignees
AlexWaygood
Labels
type-featureA feature request or enhancement
@AlexWaygood

Description

@AlexWaygood

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.

Linked PRs

Metadata

Metadata

Assignees

Labels

type-featureA feature request or enhancement

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp