Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Python 3.11.9 catches all exceptions raised in Generic.__setattr__("__orig_class__", type_parameter) #117744

Closed as not planned
@nanne-aben

Description

@nanne-aben

Bug report

Bug description:

In Python 3.11.9, the following code does not raise an Exception:

from typing import Any, Generic, TypeVar

T = TypeVar("T")


class DataSet(Generic[T]):
    def __setattr__(self, name: str, value: Any) -> None:
        object.__setattr__(self, name, value)

        if name == "__orig_class__":
            raise Exception("Just randomly raising an exception for illustrative purposes.")


class Schema:
    pass


# initializing an object with type parameter `Schema` will call 
# `__setattr__("__orig_class__", (Schema,))`
df = DataSet[Schema]()

In other versions of Python (e.g. 3.11.8 or 3.12.2) this raises an Exception, but in Python 3.11.9 it appears this Exception is caught.

I use something similar to the above logic in typedspark to check whether the DataSet follows the schema specified in Schema, and I raise an Exception if it doesn't. As of Python 3.11.9, these exceptions are caught, hence breaking my logic.

Thank you for looking into this!

CPython versions tested on:

3.9, 3.10, 3.11, 3.12

Operating systems tested on:

Linux, macOS, Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.11only security fixes3.12only security fixes3.13bugs and security fixestopic-typingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions