Content-Length: 301729 | pFad | https://github.com/python/cpython/issues/124498

ad TypeAliasType is always generic when `type_params=()` is passed. · Issue #124498 · python/cpython · GitHub
Skip to content

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions









    ApplySandwichStrip

    pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


    --- a PPN by Garber Painting Akron. With Image Size Reduction included!

    Fetched URL: https://github.com/python/cpython/issues/124498

    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy