high-energy-55500
07/19/2023, 10:11 AMmypy
to use types-beautifulsoup4
stubs but I can’t seem to get it configured properly
🧵high-energy-55500
07/19/2023, 10:11 AMpoetry run mypy src
with the following dev dependencies declared in `pyproject.toml`:
[tool.poetry.group.dev.dependencies]
mypy = "^1.4.0"
types-beautifulsoup4 = "^4.12.0.5"
high-energy-55500
07/19/2023, 10:11 AM$ pants check ::
...
10:55:20.00 [ERROR] Completed: Typecheck using MyPy - mypy - mypy failed (exit code 2).
src/example.py:9: error: Library stubs not installed for "bs4" [import]
src/example.py:10: error: Library stubs not installed for "bs4.element" [import]
src/example.py:10: note: Hint: "python3 -m pip install types-beautifulsoup4"
src/example.py:10: note: (or run "mypy --install-types" to install all missing stub packages)
src/example.py:10: note: See <https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports>
Found 2 errors in 1 file (checked 4 source files)
high-energy-55500
07/19/2023, 10:11 AMpants.toml
[python.resolves]
mypy = "3rdparty/python/mypy.lock"
[mypy]
install_from_resolve = "mypy"
requirements =["//3rdparty/python:mypy"]
and 3rdparty/python/mypy-requirements.txt
mypy>=1.0,<2.0
types-beautifulsoup4>=4.12.0.5,<5.0
types-pytz>=2023.3.0.0,<2024.0
types-requests>=2.31.0.1,<3.0
and 3rdparty/python/BUILD
python_requirements(
name="mypy",
source="mypy-requirements.txt",
resolve="mypy",
)
high-energy-55500
07/19/2023, 10:11 AMmypy.ini
set up, but using config = "mypy.ini"
and the following mypy.ini
[mypy]
plugins =
bs4-stubs
gives me a different error
$ pants check ::
...
11:09:10.75 [ERROR] Completed: Typecheck using MyPy - mypy - mypy failed (exit code 2).
Found 1 error in 1 file (errors prevented further checking)
mypy.ini:2: error: Plugin "bs4-stubs" does not define entry point function "plugin" [misc]
high-energy-55500
07/19/2023, 10:12 AMbitter-ability-32190
07/19/2023, 11:23 AMbitter-ability-32190
07/19/2023, 11:24 AMhigh-energy-55500
07/19/2023, 3:36 PMmypy.lock
/ mypy-requirements.txt
for mypy
then? only mypy
specifically, and it’s ignoring the remaining reqs i have in the requirements file unless I specifically tell it to include them?high-energy-55500
07/19/2023, 3:37 PMhigh-energy-55500
07/19/2023, 3:37 PMoverrides
and type_stubs_module_mapping
, and neither seem to work:
python_requirements(
name="mypy",
source="mypy-requirements.txt",
resolve="mypy",
type_stubs_module_mapping={"types-beautifulsoup4": ["bs4"]},
overrides={
"mypy": {
"dependencies": ["3rdparty/python:mypy#types-beautifulsoup4"],
},
}
)
bitter-ability-32190
07/19/2023, 3:42 PMpants --keep-sandboxes=on_failure ...
and then poke at the sandboxhigh-energy-55500
07/19/2023, 3:59 PMmypy.pex/PEX-INFO
file in the sandbox which would seem to me to indicate that types-beautifulsoup4
is included:
{
"bootstrap_hash": "774e132021b0a8c19e8a935452a4415d5ec16f93",
"build_properties": {
"pex_version": "2.1.134"
},
"code_hash": "f3331d84c5b30eaf04417cd3fc0dc95864d7937f",
"distributions": {
"mypy-1.4.1-cp311-cp311-macosx_11_0_arm64.whl": "b06c414ddea0c6df5d6cfa741320a8dc78150754ece26d7be790a48ce586c208",
"mypy_extensions-1.0.0-py3-none-any.whl": "322faffb2e4c7768d03d354528240ded8adea9dd0e6e67d998f275994c744cb5",
"types_beautifulsoup4-4.12.0.5-py3-none-any.whl": "64549893d677f5e3d2852ae45dbdda1a0d6ff63a1163db239acf9f4b2362ce7b",
"types_html5lib-1.1.11.14-py3-none-any.whl": "8e8e95bd3b28462ed28c210b87535163b8a0ececc162384cf17cd48e6e77b93a",
"types_pytz-2023.3.0.0-py3-none-any.whl": "77afe64369f5fd5368aa7ab394a45bee5fdd4b2a76c778994fa5d0cd5c0f0c94",
"types_requests-2.31.0.1-py3-none-any.whl": "f5f50d985bd991f221b7cb5ee2777e45325b574d974042ace3d0744ab6a54273",
"types_urllib3-1.26.25.13-py3-none-any.whl": "7aca16f70169e31fbd943b512c72b50d06ba6f33e31144c33c127f53718154dc",
"typing_extensions-4.7.1-py3-none-any.whl": "5e6df9210d60789205e617f60bb181074200092ebed2796a63358a9c9f3f1c75"
},
"emit_warnings": false,
"entry_point": "mypy.__main__:console_entry",
"ignore_errors": false,
"includes_tools": true,
"inherit_path": "false",
"inject_args": [],
"inject_env": {},
"interpreter_constraints": [],
"pex_hash": "ff22d350f24b4d9d4161cc7a3d91455d58af1f08",
"pex_path": "",
"pex_paths": [],
"requirements": [
"mypy<2.0,>=1.0",
"types-beautifulsoup4<5.0,>=4.12.0.5",
"types-pytz<2024.0,>=2023.3.0.0",
"types-requests<3.0,>=2.31.0.1"
],
"strip_pex_env": true,
"venv": true,
"venv_bin_path": "false",
"venv_copies": false,
"venv_hermetic_scripts": true,
"venv_site_packages_copies": true
}
bitter-ability-32190
07/19/2023, 4:02 PMmypy
isn't checking its own env for requirements? And just checking the env of the provided Pythonhigh-energy-55500
07/19/2023, 4:08 PMpoetry_requirements(
name="root",
overrides={
"beautifulsoup4": {
"dependencies": ["//:root#types-beautifulsoup4"],
},
}
)
bitter-ability-32190
07/19/2023, 4:09 PMhigh-energy-55500
07/19/2023, 4:09 PMpython_requirements(
name="mypy",
source="mypy-requirements.txt",
resolve="mypy",
overrides={
"beautifulsoup4": {
"dependencies": ["3rdparty/python:mypy#types-beautifulsoup4"],
},
}
)
gives
$ pants check ::
17:08:07.38 [ERROR] 1 Exception encountered:
Engine traceback:
in `check` goal
in Find targets from input specs
InvalidFieldException: Unused key in the `overrides` field for 3rdparty/python:mypy: ['beautifulsoup4']
bitter-ability-32190
07/19/2023, 4:10 PMbeautifulsoup
isn't a top-level requirement in your reqs file. I don't think that'd work as expected either.
Anyways, seems like you have enough to go from here?high-energy-55500
07/19/2023, 4:15 PMThat makes sense,you mean addingisn’t a top-level requirement in your reqs file.beautifulsoup
beautifulsoup4
to mypy-requirements.txt
? unfortunately that seems to give me the same error as before for the missing library stubs
this seems functional, if not the most elegant solution, but I’ll take what I can get. so your recommendation would be to just add it to the overrides in the root dependencies then?bitter-ability-32190
07/19/2023, 4:16 PMbitter-ability-32190
07/19/2023, 4:17 PMextra_type_stubs
deprecation should be un-deprecated? https://www.pantsbuild.org/docs/reference-mypy#extra_type_stubs)
There's also this: https://www.pantsbuild.org/docs/reference-python_requirement#codetype_stub_modulescode so maybe try that?high-energy-55500
07/19/2023, 4:23 PMextra_type_stubs
seemed to do the trick!
so now I have pants.toml
:
[mypy]
install_from_resolve = "mypy"
extra_type_stubs = [
"types-beautifulsoup4",
]
requirements =["//3rdparty/python:mypy"]
3rdparty/python/mypy-requirements.txt
:
mypy>=1.0,<2.0
types-beautifulsoup4>=4.12.0.5,<5.0
types-pytz>=2023.3.0.0,<2024.0
types-requests>=2.31.0.1,<3.0
`3rdparty/python/BUILD`:
python_requirements(
name="mypy",
source="mypy-requirements.txt",
resolve="mypy",
)
high-energy-55500
07/19/2023, 4:23 PMbitter-ability-32190
07/19/2023, 4:23 PMbitter-ability-32190
07/19/2023, 4:23 PMbitter-ability-32190
07/19/2023, 4:24 PMhigh-energy-55500
07/19/2023, 4:27 PMtype_stub_modules
seems to be exclusive to python_requirement
and not python_requirements
fwiw
if i’m doing it right, it doesn’t seem to work:
python_requirement(
name="mypy",
requirements=[
"mypy>=1.0,<2.0",
"types-beautifulsoup4>=4.12.0.5,<5.0",
"types-pytz>=2023.3.0.0,<2024.0",
"types-requests>=2.31.0.1,<3.0",
],
resolve="mypy",
type_stub_modules=["types-beautifulsoup4"]
)
bitter-ability-32190
07/19/2023, 4:28 PMpython_requirements
is a generator generating python_requirement
targets
and you'd need all this on the right target(s) for your normal user resolve. Not the mypy onebitter-ability-32190
07/19/2023, 4:28 PMhigh-energy-55500
07/19/2023, 4:32 PMmypy
target? it still complains about the missing stub when using it this waybillions-printer-97253
07/20/2023, 10:39 PMextra_type_stubs
in the pants.toml
file.
Hopefully this gets flushed out a bit before deprecation.bitter-ability-32190
07/20/2023, 10:40 PMincalculable-beach-72723
08/16/2023, 6:52 PMextra_type_stubs
(and their lockfile) in place for mypy to continue working - putting the stubs in the mypy resolver resulted both in them being included in the mypy pex (verified in the sandbox), and not being used during checkinghigh-energy-55500
08/25/2023, 11:39 AMdry-architect-80370
09/05/2023, 7:44 PMhe requirements would be on your user resolve NOT the mypy resolve. E.g. where your bs4 requirement isThis works for me
best-furniture-15331
05/01/2024, 8:13 PMextra_type_stubs
anymore. I've set up a mypy resolve based on the lockfile docs, and I can see that it's correctly resolving the dependencies when running check
, but I still get error: Library stubs not installed for <various libs> [import]
dry-architect-80370
05/02/2024, 10:13 AMbest-furniture-15331
05/02/2024, 8:14 PMpants.toml
[GLOBAL]
pants_version = "2.20.0"
backend_packages = [
"pants.backend.python",
"pants.backend.python.lint.autoflake",
"pants.backend.python.lint.black",
"pants.backend.python.lint.isort",
"pants.backend.python.lint.flake8",
"pants.backend.python.typecheck.mypy",
"pants.backend.google_cloud_function.python",
"pants.backend.shell",
"pants.backend.shell.lint.shellcheck",
"pants.backend.shell.lint.shfmt",
"pants.backend.codegen.protobuf.python",
"pants.backend.docker",
]
[python]
enable_resolves = true
default_resolve = "common"
interpreter_constraints = ["CPython==3.9.*"]
[python.resolves]
common = "3rdparty/python/common.lock"
mypy = "3rdparty/python/mypy.lock"
[mypy]
install_from_resolve = "mypy"
requirements = ["//3rdparty/python:mypy"]
[python-protobuf]
mypy_plugin = true
3rdparty/python/BUILD
python_requirements()
python_requirements(
name="mypy",
source="mypy-requirements.txt",
resolve="mypy",
)
3rdparty/python/mypy-requirements.txt
mypy==1.5.0
types-protobuf==3.20.2
types-redis==4.3.13
types-requests==2.28.0
dry-architect-80370
05/02/2024, 8:37 PMrequirements
entry from the mypy
section of pants.toml
. In your current setup you are telling Pants that you only need the 'mypy' requirement from that resolve, and it discards the rest.best-furniture-15331
05/02/2024, 9:11 PMrequirements
entry again and re-ran but same error:
>> pants --changed-since=origin/main --changed-dependents=transitive check
[...]
error: Library stubs not installed for "redis"
[...]
I also confirmed that e.g. types-redis
is in the mypy.lock
file
[...]
"requirements": [
"mypy==1.5.0",
"types-protobuf==3.20.2",
"types-redis==4.3.13",
"types-requests==2.28.0",
],
[...]
dry-architect-80370
05/03/2024, 8:49 AMIf you install MyPy from a custom lockfile you can also add type stub requirements to that lockfile. This ensures that the stubs are only used when running MyPy and are not included when, for example, packaging a PEX.
I have checked, and the same thing happens to me, I need to add types-redis
to the default requirements (not only the MyPy ones) to make it work.
Minimal repro: https://github.com/kordek/pants-mypy-reqs-reprobest-furniture-15331
05/07/2024, 10:27 PMcommon
requirements file?steep-airline-70735
06/11/2024, 8:08 PM...
transformers==4.40.2
types-protobuf==3.20.3
types-psycopg2==2.9.5
types-PyYAML==6.0.12.2
types-redis==4.3.13
types-requests==2.28.0
types-setuptools==57.4.4
uvicorn==0.21.0
When running:
pants --changed-since=origin/main --changed-dependents=transitive check
...
src/python/common/db/records/paper_records_test.py:9:1: error: Library stubs not installed for "google.protobuf.json_format" [import]
from google.protobuf.json_format import MessageToJson
Running pants dependencies --transitive src/python/common/db/records/paper_records_test.py
, I see that types-protobuf is not inferred:
3rdparty/python#SQLAlchemy
3rdparty/python#aiopg
3rdparty/python#loguru
3rdparty/python#protobuf
3rdparty/python#psycopg2-binary
3rdparty/python#pyarrow
3rdparty/python#pydantic
3rdparty/python#testcontainers
3rdparty/python#types-psycopg2
3rdparty/python/requirements.txt
src/infra/db:main_sql
src/protos/paper_metadata.proto
src/python/common/beam/records/base_record.py
src/python/common/config/__init__.py
src/python/common/config/constants.py
src/python/common/db/__init__.py
src/python/common/db/execute.py
src/python/common/db/papers_util.py
src/python/common/db/records/paper_records.py
src/python/common/db/test/connect.py
src/python/common/time/convert.py
modern-thailand-62200
08/09/2024, 1:32 PMpants check