theoremlp / rules_mypy

a Bazel mypy aspect
Apache License 2.0
16 stars 4 forks source link

Errors from third party packages (e.g. numpy) #15

Closed jimmyt857 closed 2 months ago

jimmyt857 commented 2 months ago

Hello! First of all I'd like to express appreciation for the project and your time. MyPy type checking is a requirement for our team and we're hoping to retain all of the Bazel optimizations while using it.

I'm trying to onboard and ran into 2 problems (I'll create another issue to track the other problem). When importing numpy==1.26.4, I got lots of errors. Here's a small subset:

external/rules_python~~pip~our_env1_312_numpy_cp312_cp312_manylinux_2_17_x86_64_675d61ff/site-packages/numpy/__init__.pyi:4242: error: Signature of "partition" incompatible with supertype "ndarray"  [override]
external/rules_python~~pip~our_env1_312_numpy_cp312_cp312_manylinux_2_17_x86_64_675d61ff/site-packages/numpy/__init__.pyi:4242: note:      Superclass:
external/rules_python~~pip~our_env1_312_numpy_cp312_cp312_manylinux_2_17_x86_64_675d61ff/site-packages/numpy/__init__.pyi:4242: note:          def partition(self, kth: _SupportsArray[dtype[bool_ | integer[Any]]] | _NestedSequence[_SupportsArray[dtype[bool_ | integer[Any]]]] | bool | int | _NestedSequence[bool | int], axis: SupportsIndex = ..., kind: Literal['introselect'] = ..., order: str | Sequence[str] | None = ...) -> None
external/rules_python~~pip~our_env1_312_numpy_cp312_cp312_manylinux_2_17_x86_64_675d61ff/site-packages/numpy/__init__.pyi:4242: note:      Subclass:
external/rules_python~~pip~our_env1_312_numpy_cp312_cp312_manylinux_2_17_x86_64_675d61ff/site-packages/numpy/__init__.pyi:4242: note:          @overload
external/rules_python~~pip~our_env1_312_numpy_cp312_cp312_manylinux_2_17_x86_64_675d61ff/site-packages/numpy/__init__.pyi:4242: note:          def partition(self, sep: _SupportsArray[dtype[str_]] | _NestedSequence[_SupportsArray[dtype[str_]]] | str | _NestedSequence[str]) -> chararray[Any, dtype[str_]]
external/rules_python~~pip~our_env1_312_numpy_cp312_cp312_manylinux_2_17_x86_64_675d61ff/site-packages/numpy/__init__.pyi:4242: note:          @overload
external/rules_python~~pip~our_env1_312_numpy_cp312_cp312_manylinux_2_17_x86_64_675d61ff/site-packages/numpy/__init__.pyi:4242: note:          def partition(self, sep: _SupportsArray[dtype[bytes_]] | _NestedSequence[_SupportsArray[dtype[bytes_]]] | bytes | _NestedSequence[bytes]) -> chararray[Any, dtype[bytes_]]
external/rules_python~~pip~our_env1_312_numpy_cp312_cp312_manylinux_2_17_x86_64_675d61ff/site-packages/numpy/lib/scimath.pyi:18: error: Overloaded function signatures 2 and 3 overlap with incompatible return types  [overload-overlap]
external/rules_python~~pip~our_env1_312_numpy_cp312_cp312_manylinux_2_17_x86_64_675d61ff/site-packages/numpy/lib/scimath.pyi:18: error: Overloaded function signatures 2 and 4 overlap with incompatible return types  [overload-overlap]
external/rules_python~~pip~our_env1_312_numpy_cp312_cp312_manylinux_2_17_x86_64_675d61ff/site-packages/numpy/lib/scimath.pyi:27: error: Overloaded function signatures 2 and 3 overlap with incompatible return types  [overload-overlap]

Steps to reproduce: I was able to reproduce this in the example from this repo (examples/demo) with the following modifications:

  1. numpy==1.26.4 added to requirements.in.
  2. print(np.__version__) in any of the python files.

Expected result: No errors; my code passes mypy checks, it is only the 3p contents that fail.

Actual Result: Lots of errors.

Perhaps there could be an option to skip type-checking third-party libraries, or a recommended set of options for mypy.ini?

mark-thm commented 2 months ago

I'd recommend setting follow_imports = silent in mypy.ini. We should probably default this in the ruleset with arguments to mypy, though.

mark-thm commented 2 months ago

It looks like you need both follow_imports = silent and follow_imports_for_stubs = True, we'll update our default mypy.ini but there's unfortunately no way to fix this on the command line, so if you override mypy.ini you'll need both of these.

jimmyt857 commented 2 months ago

Awesome, this worked for me. Thank you!