Skip to content

Log a warning message if we can detect that ty Python version doesn't match active environment Python version #1708

@alex

Description

@alex

Summary

In pyca/cryptography we have code like:

if sys.version_info >= (3, 11):
    import tomllib
else:
    import tomli as tomllib

This produces errors like:

error[unresolved-import]: Cannot resolve imported module `tomli`
  --> noxfile.py:21:12
   |
19 |     import tomllib
20 | else:
21 |     import tomli as tomllib
   |            ^^^^^^^^^^^^^^^^
22 |
23 | nox.options.reuse_existing_virtualenvs = True
   |
info: Searched in the following paths during module resolution:
info:   1. /Users/alex_gaynor/projects/cryptography/src (first-party code)
info:   2. /Users/alex_gaynor/projects/cryptography (first-party code)
info:   3. vendored://stdlib (stdlib typeshed stubs vendored by ty)
info:   4. /Users/alex_gaynor/projects/cryptography/.nox/flake/lib/python3.14/site-packages (site-packages)
info:   5. /Users/alex_gaynor/projects/cryptography/vectors (editable install)
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
info: rule `unresolved-import` is enabled by default

As far as I can tell what's happening is: ty is in a Python 3.14 venv, so tomli isn't installed. However, ty infers that it should run for Python 3.8 from my project's requires-python.

Therefore it type-checks the import tomli path and is said that the package isn't installed.

I'm not quite sure what the right behavior is, but the current behavior is perplexing and its not clear how to fix :-)

Version

ty 0.0.1-alpha.29 (0c3cae4 2025-11-28)

Metadata

Metadata

Assignees

No one assigned

    Labels

    diagnosticsRelated to reporting of diagnostics.importsModule resolution, site-packages discovery, import-related diagnostics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions