Exciting news: The mypy implementation of TypeForm is now ready for testing by library maintainers and other users! 
To use mypy with TypeForm support:
# Install mypy with TypeForm support
pip install git+https://github.com/davidfstr/mypy@170a5e70dfb03425c98ba53b5ecb9b7def6e3f65 # >= 2025-03-25
# Install typing_extensions with TypeForm support
pip install 'typing_extensions>=4.13.0'
# Run mypy with TypeForm support enabled
mypy --enable-incomplete-feature=TypeForm
I’m continuing to shepherd the PR that merges this support into mypy’s main branch and eventually into a released version of mypy.
Additionally pyright >= 1.1.395 supports the latest draft of the TypeForm PEP, when you set “enableExperimentalFeatures” to true in the pyright configuration. So you can test how TypeForm behaves with pyright too.
One rough edge was found during the implementation: mypy cannot recognize quoted type expressions (like 'str | None'
or list['str']
) as TypeForm literals implicitly in all syntactic locations. Only 3 presumably-common locations (assignment r-values, callable arguments, and returned expressions) are currently supported:
# 1. Assignment r-value
typx: TypeForm = 'str | None' # OK
# 2. Callable argument
def accept_typeform(typx: TypeForm) -> None: ...
accept_typeform('str | None') # OK
# 3. Returned expression
def return_typeform() -> TypeForm:
return 'str | None' # OK
# Everywhere else...
list_of_typx: list[TypeForm] = ['str | None'] # ERROR
dict_of_typx: dict[str, TypeForm] = {'str_or_none': 'str | None'} # ERROR
When using a quoted type expression in other locations, mypy gives an error that reads:
TypeForm containing a string annotation cannot be recognized here. Surround with TypeForm(...) to recognize.
To avoid errors you must tweak the code to use the explicit TypeForm(...)
syntax:
# Everywhere else... (Take 2)
list_of_typx: list[TypeForm] = [TypeForm('str | None')] # OK
dict_of_typx: dict[str, TypeForm] = {'str_or_none': TypeForm('str | None')} # OK
I view this current limitation in the mypy implementation as a small deviation from the PEP, which implies a string literal expression containing a valid type expression should be assignable to TypeForm regardless of syntactic position.
Any comments on this limitation in general? Any comments on the current error message?