Skip to content

feat: add type statement support (PEP-695, Python 3.12 and 3.13)#31

Merged
wookayin merged 5 commits intomasterfrom
type-statement
Jul 12, 2025
Merged

feat: add type statement support (PEP-695, Python 3.12 and 3.13)#31
wookayin merged 5 commits intomasterfrom
type-statement

Conversation

@wookayin
Copy link
Owner

@wookayin wookayin commented Jun 30, 2025

Overview

  • Basic type statement support (3.12), any following code should not lose their variable scope
  • bound and default parameter support (3.13)
  • The new generic syntax, e.g. class Generic[T], def generic_method[T], def func[T](...), etc. (3.12)

References:

Example 1:

type MyList[T] = list[T]

def foo():
   mylist: MyList[int] = [1, 2, 3]   # semshi used to break here

Example 2: bound and default (python 3.13):

type Alias1[T, P] = list[P] | set[T]
type Alias2[T, P: type[T]] = list[P] | set[T]
type Alias3[T, P = T] = list[P] | set[T]
type Alias4[T: int, P: int = bool | T] = list[P] | set[T]

Example 3: generic syntax

def get_first[T: float](data: list[T]) -> T:
   ...

class Foo[**P, R]:
    fn: Callable[P, R]
    def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R:
        ...

@wookayin wookayin mentioned this pull request Dec 6, 2023
2 tasks
@wookayin wookayin changed the title feat: add type statement support (python 3.12 and 3.13) feat: add type statement support (PEP-695, Python 3.12 and 3.13) Jun 30, 2025
@wookayin wookayin mentioned this pull request Jun 30, 2025
wookayin added 2 commits July 1, 2025 08:42
When a type statement is present, all the variable scopes after the
statement were broken. Semshi now handles the type parameter syntax
(PEP-695, Python 3.12+) and its variable scope correctly.
@wookayin wookayin added this to the 0.4.0 milestone Jul 2, 2025
wookayin added 2 commits July 2, 2025 22:54
- bound (e.g. `T: int`), a part of PEP-695 (python 3.12)
- default value (e.g. `T = bool`), introduced in python 3.13

Both has their own variable scope within type statement, which should be
handled properly to not break the following scopes.
@wookayin wookayin marked this pull request as ready for review July 7, 2025 22:29
@wookayin
Copy link
Owner Author

wookayin commented Jul 8, 2025

Parser and AST visitation works fine now, but semantic reference tracking within (nested) lexical scopes is not quite accurate:

e.g.

def func1[**P, R](func: Callable[P, R], /, *args: *tuple[()]) -> Callable[P, R]: ...

We could still merge the basic support and later fix the bug as a follow-up.

@wookayin wookayin merged commit 0a14fc2 into master Jul 12, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant