GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Improve error message from `os.fspath` if `__fspath__` is set to `None` · Issue #106046 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed
#106082
Closed
Improve error message from os.fspath if __fspath__ is set to None#106046
#106082
Copy link
Assignees
AlexWaygood
Labels
type-featureA feature request or enhancementA feature request or enhancement

Description

@AlexWaygood
AlexWaygood
opened on Jun 23, 2023
Issue body actions

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

  • gh-106046: Improve error message from os.fspath if __fspath__ is set to None #106082

Activity

  1. AlexWaygood
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Jun 23, 2023
  2. barneygale commented on Jun 23, 2023

    @barneygale
    barneygale
    on Jun 23, 2023
    Contributor
    More actions

    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
  3. AlexWaygood commented on Jun 23, 2023

    @AlexWaygood
    AlexWaygood
    on Jun 23, 2023
    MemberAuthor
    More actions

    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__.

    Reacted by Barney Gale
  4. AlexWaygood
    self-assigned this
    on Jun 23, 2023
  5. AlexWaygood
    added a commit that references this issue on Jun 25, 2023

    pythongh-106046: Improve error message from `os.fspath` if `__fspath_…

    3ec02c8
  6. AlexWaygood
    closed this as completedin #106082on Jun 25, 2023
  7. AlexWaygood
    added a commit that references this issue on Jun 25, 2023

    gh-106046: Improve error message from `os.fspath` if `__fspath__` is …

    93a970f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @AlexWaygood
    AlexWaygood

Labels

type-featureA feature request or enhancementA feature request or enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402520 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.