Skip to content

TypeAliasType is always generic when type_params=() is passed. #124498

Closed

Description

@Daraan

Bug report

Bug description:

I realized that the type_params parameter of TypeAliasType behaves differently when it is omitted vs passing an empty tuple (), in the latter case the resulting instance is always subscriptable. I doubt this is an undocumented feature.

from typing import List, TypeAliasType, TypeVar

# < Errors like expected >

Simple = TypeAliasType("Simple", int)
try:
    Simple[int]
except TypeError:
   print("Expected: Simple is not subcriptable")

# Not allowed assignment
T = TypeVar('T')
MissingTypeParamsErr = TypeAliasType("MissingTypeParamsErr", List[T])
assert MissingTypeParamsErr.__type_params__ == ()
assert MissingTypeParamsErr.__parameters__ == ()

try:
    MissingTypeParamsErr[int]
except TypeError:
   print("Expected: Simple is not subcriptable")

# < unexpected cases >
    
MissingTypeParams = TypeAliasType("MissingTypeParams", List[T], type_params=())
assert MissingTypeParams.__type_params__ == ()
assert MissingTypeParams.__parameters__ == ()
# These cases do not raise an error
MissingTypeParams[int]
MissingTypeParams[[]]
MissingTypeParams[()]

# This also do not raise an error
Simple2 = TypeAliasType("Simple2", int, type_params=())
assert Simple2 .__type_params__ == ()
assert Simple2 .__parameters__ == ()
Simple2[int]
Simple2[[]]
Simple2[()]

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Sep 25, 2024
  2. added 2 commits that reference this issue on Sep 25, 2024
  3. added 2 commits that reference this issue on Sep 26, 2024
  4. added a commit that references this issue on Sep 26, 2024
  5. added a commit that references this issue on Sep 30, 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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions