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

typing: tested TypeVar instance subclass TypeError is incidental #90800

Closed
GBeauregard mannequin opened this issue Feb 4, 2022 · 7 comments · Fixed by #31148
Closed

typing: tested TypeVar instance subclass TypeError is incidental #90800

GBeauregard mannequin opened this issue Feb 4, 2022 · 7 comments · Fixed by #31148
Assignees
Labels
stdlib Python modules in the Lib dir topic-typing type-feature A feature request or enhancement

Comments

@GBeauregard
Copy link
Mannequin

GBeauregard mannequin commented Feb 4, 2022

BPO 46642
Nosy @serhiy-storchaka, @JelleZijlstra, @sobolevn, @Fidget-Spinner, @AlexWaygood, @GBeauregard
PRs
  • bpo-46642: explicitly disallow typing.TypeVar instance subclassing #31148
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2022-02-04.21:59:21.151>
    labels = ['type-feature', 'library']
    title = 'typing: tested TypeVar instance subclass TypeError is incidental'
    updated_at = <Date 2022-03-12.03:39:46.337>
    user = 'https://github.com/GBeauregard'

    bugs.python.org fields:

    activity = <Date 2022-03-12.03:39:46.337>
    actor = 'JelleZijlstra'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2022-02-04.21:59:21.151>
    creator = 'GBeauregard'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 46642
    keywords = ['patch']
    message_count = 7.0
    messages = ['412544', '412589', '412591', '412627', '412646', '412661', '414970']
    nosy_count = 6.0
    nosy_names = ['serhiy.storchaka', 'JelleZijlstra', 'sobolevn', 'kj', 'AlexWaygood', 'GBeauregard']
    pr_nums = ['31148']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue46642'
    versions = []

    @GBeauregard
    Copy link
    Mannequin Author

    GBeauregard mannequin commented Feb 4, 2022

    def test_cannot_subclass_vars(self):
    with self.assertRaises(TypeError):
    class V(TypeVar('T')):
    pass

    typing's testcases contain the following test to ensure instances of TypeVar cannot be subclassed:

    def test_cannot_subclass_vars(self):
        with self.assertRaises(TypeError):
            class V(TypeVar('T')):
                pass

    The reason this raises a TypeError is incidental and subject to behavior change, not because doing so is prohibited per se; what's happening is the class creation does the equivalent of type(TypeVar('T')(name, bases, namespace), but this calls TypeVar's __init__ function with these items as the TypeVar constraints. TypeVar runs typing._type_check on the type constraints passed to it, and the literals for the namespace/name do not pass the callable() check in typing._type_check, causing it to raise a TypeError. I find it dubious this is the behavior the testcase is intending to test and the error it gives is confusing

    I propose we add __mro_entries__ to the TypeVar class that only contains a raise of TypeError to properly handle this case

    I can write this patch

    @GBeauregard GBeauregard mannequin added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Feb 4, 2022
    @GBeauregard
    Copy link
    Mannequin Author

    GBeauregard mannequin commented Feb 5, 2022

    The reason this test passed before is a bit confusing. Run the following code standalone to see where the type(TypeVar('T'))(name, bases, namespace) check is coming from.

    class TypeVar:
        def __init__(self, name, *constraints):
            # in actual TypeVar, each constraint is run through
            # typing._type_check, casuing TypeError via not callable() 
            print(repr(constraints))
    
    class V(TypeVar("T")):
        pass
    

    @GBeauregard
    Copy link
    Mannequin Author

    GBeauregard mannequin commented Feb 5, 2022

    Fixing this test unblocks bpo-46644

    @serhiy-storchaka
    Copy link
    Member

    Note that instances of most other types are non-subclassable "by accident".

    >>> class A(42): pass
    ... 
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: int() takes at most 2 arguments (3 given)
    
    >>> class B:
    ...     def __init__(self, *args): pass
    ... 
    >>> class C(B()): pass
    ... 
    >>> C
    <__main__.B object at 0x7fdcfb49aae0>

    It is okay until we decide that there is a problem, and it that case it would require more general solution.

    Are there any issues with this in real code?

    @GBeauregard
    Copy link
    Mannequin Author

    GBeauregard mannequin commented Feb 6, 2022

    The issue in real code I had in mind was internal to cpython: if we remove the callable() check from _type_check naively, this test starts to fail. Both of our proposed changes happen to not fail this check. Given your second example, would you prefer if we remove this testcase if the issue comes up in the final proposed patches? On the other hand, there is precedent for giving this a nice error message other places in typing.py.

    @serhiy-storchaka
    Copy link
    Member

    The test is good. If we accidentally make a TypeVar instance subclassable, it will catch such error.

    @JelleZijlstra
    Copy link
    Member

    This still behaves similarly after the bpo-46642 fix:

    >>> class V(TypeVar("T")): pass
    ... 
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Users/jelle/py/cpython/Lib/typing.py", line 906, in __init__
        self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/Users/jelle/py/cpython/Lib/typing.py", line 906, in <genexpr>
        self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
                                     ^^^^^^^^^^^^^^^^^^^
      File "/Users/jelle/py/cpython/Lib/typing.py", line 189, in _type_check
        raise TypeError(f"{msg} Got {arg!r:.100}.")
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    TypeError: TypeVar(name, constraint, ...): constraints must be types. Got (~T,).

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir topic-typing type-feature A feature request or enhancement
    Projects
    None yet
    Development

    Successfully merging a pull request may close this issue.

    3 participants