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 — Relax TypeVar default ordering constraints · Issue #118772 · python/cpython

Relax TypeVar default ordering constraints #118772

New issue
Copy link
New issue
Copy link
Closed
Closed
Relax TypeVar default ordering constraints#118772
Copy link
Labels
3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixestopic-typingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@JelleZijlstra
JelleZijlstra
opened on May 8, 2024
Issue body actions

Bug report

Bug description:

% ./python.exe                     
Python 3.14.0a0 (heads/main-dirty:891fd12e52, May  8 2024, 09:02:37) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from typing import *
>>> T = TypeVar("T")
>>> T_dflt = TypeVar("T_dflt", default=int)
>>> Callable[[T_dflt], T]
Traceback (most recent call last):
  File "<python-input-3>", line 1, in <module>
    Callable[[T_dflt], T]
    ~~~~~~~~^^^^^^^^^^^^^
  File "/Users/jelle/py/cpython/Lib/typing.py", line 1717, in __getitem__
    return self.__getitem_inner__(params)
           ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^
  File "/Users/jelle/py/cpython/Lib/typing.py", line 431, in inner
    return func(*args, **kwds)
  File "/Users/jelle/py/cpython/Lib/typing.py", line 1730, in __getitem_inner__
    return self.copy_with(params)
           ~~~~~~~~~~~~~~^^^^^^^^
  File "/Users/jelle/py/cpython/Lib/typing.py", line 1702, in copy_with
    return _CallableGenericAlias(self.__origin__, params,
                                 name=self._name, inst=self._inst)
  File "/Users/jelle/py/cpython/Lib/typing.py", line 1419, in __init__
    self.__parameters__ = _collect_parameters(args)
                          ~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/Users/jelle/py/cpython/Lib/typing.py", line 296, in _collect_parameters
    raise TypeError(f'Type parameter {t!r} without a default'
                    ' follows type parameter with a default')
TypeError: Type parameter ~T without a default follows type parameter with a default

This can reasonably happen if you have a generic class generic over a TypeVar with a default, and that TypeVar appears in a Callable parameter type annotation:

class X[T1=int]:
    def meth[T](self, x: Callable[[T1], T]): pass

We encountered this in Hypothesis.

cc @Gobot1234

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

  • gh-118772: Allow TypeVars without a default to follow those with a default when constructing aliases #118774
  • [3.13] gh-118772: Allow TypeVars without a default to follow those with a default when constructing aliases (GH-118774) #118776

Activity

  1. JelleZijlstra
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    topic-typing
    3.13only security fixesonly security fixes
    3.14bugs and security fixesbugs and security fixes
    on May 8, 2024
  2. JelleZijlstra commented on May 8, 2024

    @JelleZijlstra
    JelleZijlstra
    on May 8, 2024
    MemberAuthor
    More actions

    https://github.com/python/cpython/actions/runs/9004596703/job/24737976172?pr=118766 for reference. This is currently breaking our Hypothesis tests on the main branch.

  3. Gobot1234 commented on May 8, 2024

    @Gobot1234
    Gobot1234
    on May 8, 2024
    Contributor
    More actions

    I think that first example should raise but I agree that the second raising is buggy

  4. JelleZijlstra commented on May 8, 2024

    @JelleZijlstra
    JelleZijlstra
    on May 8, 2024
    MemberAuthor
    More actions

    I think that first example should raise but I agree that the second raising is buggy

    They're the same example though, in the sense that the code for Callable sees the same thing; the first one just writes it out more explicitly.

  5. Gobot1234 commented on May 8, 2024

    @Gobot1234
    Gobot1234
    on May 8, 2024
    Contributor
    More actions

    It's different as described by the binding rules. The class typevar is bound on the attribute access then the default disappears. I don't feasibly see a way to implement this at runtime however so the changes are still fine.

  6. JelleZijlstra
    added 2 commits that reference this issue on May 8, 2024

    gh-118772: Allow TypeVars without a default to follow those with a de…

    aac6b01

    [3.13] gh-118772: Allow TypeVars without a default to follow those wi…

    c6680cd
  7. AlexWaygood commented on May 11, 2024

    @AlexWaygood
    AlexWaygood
    on May 11, 2024
    Member
    More actions

    I think this is complete now, but please reopen if I'm mistaken!

    Reacted by Jelle Zijlstra
  8. AlexWaygood
    closed this as completedon May 11, 2024
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

    3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixestopic-typingtype-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.