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 — [3.12] Behavior change: __getattr__ gets called after property raises NotImplementedError · Issue #103551 · python/cpython

[3.12] Behavior change: __getattr__ gets called after property raises NotImplementedError  #103551

New issue
Copy link
New issue
Copy link
Closed
Closed
[3.12] Behavior change: __getattr__ gets called after property raises NotImplementedError #103551
Copy link
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@nicoddemus
nicoddemus
opened on Apr 14, 2023
Issue body actions

Hi,

Version: 3.12.0a7
OS: Windows

While testing pytest in 3.12 on Windows, we noticed an odd behavior that we believe might be a bug/regression.

Consider this minimal example:

class Stat:
    @property
    def owner(self):
        print("--> accessing owner")
        raise NotImplementedError("owner not implemented")

    def __getattr__(self, item):
        return getattr(1, "foobar")


s = Stat()
s.owner

(Using getattr(1, ...) there just to have an object in-place; in the real code 1 is actually another object, but the outcome is the same).

Running this in Python 3.11.2 (and previous versions), we get this output:

λ py -3.11 --version
Python 3.11.2

λ py -3.11 .tmp\reproducer.py
--> accessing owner
Traceback (most recent call last):
  File "e:\projects\pytest\.tmp\reproducer.py", line 12, in <module>
    s.owner
  File "e:\projects\pytest\.tmp\reproducer.py", line 5, in owner
    raise NotImplementedError("owner not implemented")
NotImplementedError: owner not implemented

NotImplementedError is raised from the property access, and __getattr__ is never called.

In Python 3.12.0a7 however we get:

λ py -3.12 --version
Python 3.12.0a7

λ py -3.12 .tmp\reproducer.py
--> accessing owner
Traceback (most recent call last):
  File "e:\projects\pytest\.tmp\reproducer.py", line 12, in <module>
    s.owner
  File "e:\projects\pytest\.tmp\reproducer.py", line 8, in __getattr__
    return getattr(1, "foobar")
           ^^^^^^^^^^^^^^^^^^^^
AttributeError: 'int' object has no attribute 'foobar'

The property is still being accessed (as shown by the print call), but NotImplementedError is being swallowed/ignored, and __getattr__ seems to be getting called as a fallback.


Changing the example slightly:

class Stat:
    @property
    def owner(self):
        print("--> accessing owner")
        raise NotImplementedError("owner not implemented")

    def __getattr__(self, item):
        raise AttributeError("foobar")


s = Stat()
s.owner

In Python 3.11 the behavior stays the same as before, however in Python 3.12 we now get:

λ py -3.12 .tmp\reproducer.py
--> accessing owner
Traceback (most recent call last):
  File "e:\projects\pytest\.tmp\reproducer.py", line 5, in owner
    raise NotImplementedError("owner not implemented")
NotImplementedError: owner not implemented

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "e:\projects\pytest\.tmp\reproducer.py", line 12, in <module>
    s.owner
  File "e:\projects\pytest\.tmp\reproducer.py", line 8, in __getattr__
    raise AttributeError("foobar")
          ^^^^^^^^^^^^^^^^^^^^^^^^
SystemError: <class 'AttributeError'> returned a result with an exception set

Please let us know if you need more information.

Activity

  1. nicoddemus
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Apr 14, 2023
  2. AlexWaygood commented on Apr 14, 2023

    @AlexWaygood
    AlexWaygood
    on Apr 14, 2023
    Member
    More actions

    This may have already been fixed by #103332; it looks quite similar to the regression test that was added in #103336

    Reacted by Eryk Sun and sunmy2019
  3. sobolevn commented on Apr 15, 2023

    @sobolevn
    sobolevn
    on Apr 15, 2023
    Member
    More actions

    Hi, @nicoddemus 👋

    Please, try the latest main. It should be fixed now. If so, please feel free to close this issue :)

    Reacted by Bruno Oliveira
  4. nicoddemus commented on Apr 15, 2023

    @nicoddemus
    nicoddemus
    on Apr 15, 2023
    ContributorAuthor
    More actions

    Thanks!

    Indeed I tried on the latest main (2b6f5c3) and it now works the same as Python 3.11.

    Closing the issue then, thanks!

    Reacted by Alex Waygood
  5. nicoddemus
    closed this as completedon Apr 15, 2023
  6. AlexWaygood commented on Apr 15, 2023

    @AlexWaygood
    AlexWaygood
    on Apr 15, 2023
    Member
    More actions

    Great, thanks for confirming!

  7. sunmy2019
    added a commit that references this issue on Apr 25, 2023

    add test case for pythongh-103551

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — 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.