Skip to content

Constrained TypeVar resolves to subtype instead of constraint #2370

Closed

Description

@migeed-z

Describe the Bug

When a constrained TypeVar is called with a subtype of a constraint (e.g. AnyStr = TypeVar("AnyStr", str, bytes)) pyrefly resolves to the subtype instead of the constraint. For example, passing MyStr(str) should resolve AnyStr to str, not MyStr.

`from typing import TypeVar, assert_type

AnyStr = TypeVar("AnyStr", str, bytes)

def concat(x: AnyStr, y: AnyStr) -> AnyStr:
return x + y

class MyStr(str): ...

def test(m: MyStr, s: str):
assert_type(concat(m, m), str) # pyrefly says MyStr, should be str
assert_type(concat(m, s), str) # pyrefly says MyStr and also rejects s`

Note: this unittest is already available in the codebase.

Sandbox link:

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEALqcROgOZ0Q3G6UN0AVZjABqqSgBo6qOHBh8A%2Bk2IwAOunUBBdKQDKDSnQC8g4WMoAKVSG16D1qXANTspBvACU69Zhhg6AMa46AGoDBb4iHS2%2BpJ0pFExBh50ALQAfNE6sYjqdPl0lDAMAK6U6HT4dADU8d4hUDJwdACydpZOlB5RhL31vv7uThY0UW2xjlGd3XkFTfIMSsIWQSFhI1I0Ho7JBQDEdGRFYFCkdGikzePO5wAWuCVQmHTYMOf2FXOyC0sqK8GhcI0Rzbd5dfaHUjHU7nVCXVrtaRYaRQOC4QowABWMACDGacHUIAkIBKDGgcBI5EQIAOAFUyVAIEw6GASiEycECRosH4WbwaGEFOgSjRXpZIpx0AwUhkwblPvkiqVyizrAA5EViqLAfAAX2shOJRz8p0IDFoUAoBwAClCTWc0Fg8FVVpA2GUwhBgoR1AddDA3rcGAxiHBEAB6cPGk6kQi8NjhmDocOYXABODh10Qd2UT3BcN8wyoABuqGgqGwsECwTdHo5FVwxHrFPUZAY93QqWL8jgXoqJmsAGZCABGABMBvQIF1xNQuIg3YAYtAYBRHTgCJTp0A

Activity

  1. added this to the V1 Release milestone on Feb 9, 2026
  2. added
    help wantedLarger than "good first issue", but still well-defined and ready for someone to pick up
    on Feb 9, 2026
  3. added theissue type on Feb 9, 2026
  4. removed
    help wantedLarger than "good first issue", but still well-defined and ready for someone to pick up
    on Feb 9, 2026
  5. jackulau commented on Feb 10, 2026

    @jackulau
    Contributor

    down to look into this!

  6. migeed-z commented on Feb 10, 2026

    @migeed-z
    ContributorAuthor

    down to look into this!

    thank you so much! assigned :)

  7. added 3 commits that reference this issue on Feb 11, 2026
    af76fc8
    d0bdd41
    10713e0
  8. added a commit that references this issue on Feb 14, 2026
    a876e02
  9. github-actions commented on Feb 25, 2026

    @github-actions

    This issue has someone assigned, but has not had recent activity for more than 2 weeks.

    If you are still working on this issue, please add a comment so everyone knows. Otherwise, please unassign yourself and allow someone else to take over.

    Thank you for your contributions!

  10. migeed-z commented on Apr 23, 2026

    @migeed-z
    ContributorAuthor

    This no longer errors. Closing

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

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions