Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pytype/tests/test_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,13 @@ def test_forward_reference_in_type_alias(self):
Z = List[int]
""")

@test_utils.skipBeforePy((3, 12), "type aliases are new in 3.12")
def test_use_builtin_type_alias(self):
self.Check("""
type MyType = list[str]
using_mytype: MyType = ['foo', 'bar']
""")

def test_fully_quoted_annotation(self):
self.Check("""
from typing import Optional
Expand Down
11 changes: 11 additions & 0 deletions pytype/tests/test_typing1.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ def test_generate_type_alias(self):
""",
)

@test_utils.skipBeforePy((3, 12), "type aliases are new in 3.12")
def test_generate_type_alias_312(self):
ty = self.Infer("""
type MyType = list[str]
""")
self.assertTypesMatchPytd(
ty,
# TODO: b/412616662 - Generate type MyType = list[str] instead.
"MyType = list[str]",
)

def test_protocol(self):
self.Check("""
from typing_extensions import Protocol
Expand Down
27 changes: 26 additions & 1 deletion pytype/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3871,7 +3871,32 @@ def byte_INTRINSIC_SUBSCRIPT_GENERIC(self, state):
return state

def byte_INTRINSIC_TYPEALIAS(self, state):
# TODO: b/350910471 - Implement to support PEP 695
"""This intrinsic creates a type alias and puts the result on the stack."""
# https://docs.python.org/3.12/library/dis.html states:
# The argument is a tuple of the type alias’s name, type parameters,
# and value. There's no need to use the name because there's a STORE_NAME
# opcode following the call to this intrinsic.
state, param = state.pop()
# TODO: b/412616662 - For pytd generation, it's better to generate type
# aliases as `type MyType = int`, because the type alias is not callable
# if we do Mytype(), thus there should be a diagnostic when you try to call
# a type alias. For now, we generate `type MyType = int` as `MyType = int`
# because the machinery to make a distinction is not in place yet.
# TODO: b/350910471 - support the use of typevar
_, typevar, funcv = param.data[0].pyval
args = function.Args(
posargs=(),
namedargs={},
starargs=None,
starstarargs=None,
)
_, ret = function.call_function(
self.ctx,
state.node,
funcv,
args=args,
)
state = state.push(ret)
return state

def byte_INTRINSIC_2_INVALID(self, state):
Expand Down
Loading