Skip to content

Comments

Add TypeForm (PEP 747) to the spec and conformance suite#2183

Open
JelleZijlstra wants to merge 7 commits intopython:mainfrom
JelleZijlstra:typeform
Open

Add TypeForm (PEP 747) to the spec and conformance suite#2183
JelleZijlstra wants to merge 7 commits intopython:mainfrom
JelleZijlstra:typeform

Conversation

@JelleZijlstra
Copy link
Member

No description provided.

@srittau srittau added topic: typing spec For improving the typing spec topic: conformance tests Issues with the conformance test suite labels Feb 20, 2026
Copy link
Member

@carljm carljm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me - thank you!

Copy link
Contributor

@davidhalter davidhalter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick addition, I think some cases where clarifications would be great (and that are also not really mentioned in the PEP):

def f(x: TypeForm[int | str]): ...

# Are these all fine or not? These are not normal statements and it's quite unusual to
# infer arguments in this way whereas things like `x: TypeAlias = int` are more typical.
# I personally don't care if this is inferred or not, just that it is part of the conformance tests.
f(int)
f(int | str)
f("int")

# There are obviously more complicated cases for type inference like this one:
def g(x: list[TypeForm[int]]): ...
g([int])
g(["int"])
g([TypeForm("int")])  # Obviously fine

This would obviously affect all type inference for all type checkers in quite a big way. Again, I'm just not sure what the PEP wants here, but you can probably clarify. Here I would probably also prefer that at least some of the above cases are added to the conformance tests, because otherwise we would have different behavior for inference in a feature that doesn't really need that. Type inference is already a big mess across type checkers.

v1_type_form: TypeForm[str | None] = str | None # OK

v2_actual: types.GenericAlias = list[int] # OK
v2_type_form: TypeForm = list[int] # OK
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like this case to be clarified with an additional

assert_type(v2_type_form, TypeForm[Any])

since the PEP mentions that this case is not inferred, but Any (which seems reasonable). But I would like this to be consistent across type checkers.

@davidhalter
Copy link
Contributor

Another thing that I just remember is the case of

def takes_type_form(x: TypeForm[int | str]):
a = int | str
b = "int | str"
takes_type_form(a)
takes_type_form(b)

Here I would also like some guidance. Are type checkers expected to store more information than UnionType when inferring int | str.

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

Labels

topic: conformance tests Issues with the conformance test suite topic: typing spec For improving the typing spec

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants