stocky-terabyte-10887
05/01/2024, 8:35 PMmypy
as a pants check goal, but I’m having some trouble, specifically with 3rd party plugins. My mypy.ini
file (see snippet below) declares plugins for pydantic and strawberry, both of which are defined in poetry in my pyproject.toml.
[mypy]
plugins = pydantic.mypy, strawberry.ext.mypy_plugin
The problem I’m running into is that mypy can’t import the plugin, despite having run pants generate-lockfile
recently with the most up-to-date dependencies.
mypy.ini:8:1: error: Error importing plugin "pydantic.mypy": No module named 'pydantic' [misc]
plugins = pydantic.mypy, strawberry.ext.mypy_plugin
I tried looking through past threads to understand what needs to be done to support 3rd party libraries with mypy, but I’m getting a bit lost.broad-processor-92400
05/01/2024, 9:14 PMwide-midnight-78598
05/01/2024, 9:14 PM[mypy]
lockfile = "build-support/python/mypy.lock"
version = "mypy==1.2.0"
extra_requirements = ["pydantic"]
mypy.ini
[mypy]
plugins = pydantic.mypy
stocky-terabyte-10887
05/01/2024, 9:15 PM[GLOBAL]
pants_version = "2.20.0"
colors = true
# Packages needed for pants to run various goals
backend_packages = [
"pants.backend.python",
"pants.backend.experimental.python.lint.ruff.check",
"pants.backend.experimental.python.lint.ruff.format",
"pants.backend.python.typecheck.mypy",
]
[anonymous-telemetry]
enabled = true
repo_id = "88035b17-7ce9-48d2-a168-4d5d20095e4e"
[source]
# Directory structure rooted around a shared modules directory and various services
root_patterns = [
"/shared",
"/services",
"/cron_jobs",
"/tests",
"/scripts",
"/docker/localstack"
]
[python]
# The default interpreter constraints for code in this repo. Individual targets can override
# this with the `interpreter_constraints` field. See
# <https://www.pantsbuild.org/docs/python-interpreter-compatibility>.
# Modify this if you don't have Python 3.9 on your machine.
# This can be a range, such as [">=3.8,<3.11"], but it's usually recommended to restrict
# to a single minor version.
interpreter_constraints = ["CPython==3.10.*"]
# Enable the "resolves" mechanism, which turns on lockfiles for user code. See
# <https://www.pantsbuild.org/docs/python-third-party-dependencies>. This also adds the
# `generate-lockfiles` goal for Pants to generate the lockfile for you.
enable_resolves = true
[python.resolves]
python-default = "lockfiles/python-default.lock"
[python-bootstrap]
# We search for interpreters both on the $PATH and in the `$(pyenv root)/versions` folder.
# If you're using macOS, you may want to leave off the <PATH> entry to avoid using the
# problematic system Pythons. See
# <https://www.pantsbuild.org/docs/python-interpreter-compatibility#changing-the-interpreter-search-path>.
search_path = ["<PYENV_LOCAL>"]
[ruff]
config = "ruff.toml"
[mypy]
config = "mypy.ini"
args = ["--show-error-codes", "--pretty", "--show-column-numbers"]
requirements = [
"pydantic>=2.6.3",
"sqlalchemy>=2.0.0",
"strawberry-graphql>=0.217.0",
]
wide-midnight-78598
05/01/2024, 9:33 PMmypy.ini
is?
I've upgraded my project to 2.20, it seems to still work, but when I put a mypy.ini in the "wrong" place, then everything blew up and I got the same pydantic errorstocky-terabyte-10887
05/01/2024, 9:34 PMmypy.ini
file is at the root of my project. I then have most of my code in the top-level shared and services packages.wide-midnight-78598
05/01/2024, 9:35 PMstocky-terabyte-10887
05/01/2024, 9:35 PMwide-midnight-78598
05/01/2024, 9:38 PMwide-midnight-78598
05/01/2024, 9:43 PM[mypy]
install_from_resolve = "python-default"
requirements = ["pydantic>=2.6.3"]
stocky-terabyte-10887
05/01/2024, 9:44 PMinstall_from_resolve
configuration, let me give that a shotwide-midnight-78598
05/01/2024, 9:44 PMwide-midnight-78598
05/01/2024, 9:47 PMrequirements
as I'm guessing pydantic is already in your python-default resolve? I just did a quick shot, and that caused some weird conflicts that I didn't feel like resolving.
But, I put a mypy.ini in the root folder, and then did:
[mypy]
install_from_resolve = "python-default"
and it seems to work again (confirmed that mypy was using pydantic, and when I change this plugin everything blows up)stocky-terabyte-10887
05/01/2024, 9:49 PMwide-midnight-78598
05/01/2024, 9:51 PMwide-midnight-78598
05/01/2024, 9:51 PMrequirements
option, in which case only those requirements will be installed. This is useful if you don't want to invalidate the tool's outputs when the resolve incurs changes to unrelated requirements.
mypy doesnt know about pydantic, essentially