Python version
The Python version affects allowed syntax, type definitions of the standard library, and type definitions of first- and third-party modules that are conditional on the Python version.
For example, Python 3.10 introduced support for match statements, and Python 3.15 added
sys.abi_info to the standard library. Syntactic features always need to be available in the
lowest supported Python version, but symbols may be used in a sys.version_info conditional branch.
import sys
# `invalid-syntax` error if `python-version` is set to 3.9 or lower:
match "echo hello".split():
case ["echo", message]:
print(message)
case _:
print("unknown command")
# `missing-attribute` error if `python-version` is set to 3.14 or lower:
print(sys.abi_info)
if sys.version_info >= (3, 15):
# ok, because the usage is guarded by a version check:
print(sys.abi_info)
By default, the lower bound of the project's requires-python field (from the pyproject.toml) is
used as the target Python version, ensuring that features and symbols only available in newer Python
versions are not used.
For scripts with inline metadata, ty uses the script's inline
environment.python-version if set, then the lower bound of its requires-python field. If the
inline metadata has neither setting and contains no [tool.ty] table, the script can inherit the
project's Python version.
For either scripts or larger projects, if the requires-python field is not available but a
virtual environment has been configured or detected, ty will try to infer the Python version
being used from the virtual environment's metadata.
If no virtual environment is present or inferring the Python version from the metadata fails,
ty will fall back to the default python-version.
The Python version may also be explicitly specified using the
python-version setting or the
--python-version flag.
ty officially supports type checking code that targets Python 3.10 and later, though earlier
versions (Python 3.7 through 3.9) can still be selected as targets. When analyzing code under
--python-version=3.7 or similar, ty will continue to check version-dependent syntax, perform
narrowing based on sys.version_info, and more, but may produce false positives or false
negatives for standard-library APIs as its bundled stubs do not fully describe those Python
versions.