You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Right now, we instruct MyPy to run over not only the specified files, but all transitive deps. A user found that this results in type checking more than the user actually intended. It is true that we need all transitive deps to be in the chroot, but we shouldn't be telling MyPy to run directly on those.
FYI, how it works now
We partition MyPy based on interpreter constraints, which allows for you to run over a codebase that might have some parts only Py2 and another part only Py3:
Instead, we need to figure out which files were specified by the user. How? Looking back at the below definition, the field_set_addresses represent the roots, meaning what the user explicitly said to run on. Meanwhile, closure represents the transitive closure: all roots + transitive deps.
We want to operate on the roots, i.e. the field_set_addresses. We need to get the Python source files that belong specifically to those roots, whereas now we're getting it for the whole closure. We'll use the same approach as before, sayingGet(PythonSourceFiles, PythonSourceFilesRequest(..)), which will run a rule that gets us the relevant Python source files; only, we'll run it over just the roots, rather than the whole closure.
Part 1: refactor
First, there's a refactor to facilitate this change. Rather than storing on MyPyPartition the field field_set_addresses: FrozenOrderedSet[Address], change it to root_targets: FrozenOrderedSet[Target]. We're going to need Target objects in a moment, and this is also a clearer name.
Then, fix our rule that creates the partitions. Update to combined_roots.update(transitive_targets.roots):
First, that name typechecked_sources_request is confusing now. Really, it should probably be something like closure_sources_request. We're not going to typecheck every one of those files anymore.
Then, add something like roots_sources_request = Get(PythonSourceFiles, PythonSourceFilesRequest(partition.root_targets)). Feel free to use a better var name.
Add to this await MultiGet, which is what will cause the engine to actually execute the relevant code:
"""We run over all .py and .pyi files, but .pyi files take precedence.
MyPy will error if we say to run over the same module with both its .py and .pyi files, so we
must be careful to only use the .pyi stub.
"""
result: OrderedSet[str] =OrderedSet()
forfinfiles:
iff.endswith(".pyi"):
py_file=f[:-1] # That is, strip the `.pyi` suffix to be `.py`.
result.discard(py_file)
result.add(f)
eliff.endswith(".py"):
pyi_file=f+"i"
ifpyi_filenotinresult:
result.add(f)
returntuple(result)
Testing
You'll also want to check that mypy/rules_integration_test.py works correctly still. You may need to tweak some tests.
It'd be helpful to add a simple test that ensures this behavior works correctly. We're going to be changing this MyPy code in the future and we don't want to regress. Look at how the other tests are doing it to see how to set up, and also refer to https://www.pantsbuild.org/docs/rules-api-testing.
The text was updated successfully, but these errors were encountered:
This adds MyPy to Pants for typechecking Pulumi code. We're not
introducing MyPy more broadly at this time until
pantsbuild/pants#11553 has been
addressed.
Signed-off-by: Christopher Maier <chris@graplsecurity.com>
Right now, we instruct MyPy to run over not only the specified files, but all transitive deps. A user found that this results in type checking more than the user actually intended. It is true that we need all transitive deps to be in the chroot, but we shouldn't be telling MyPy to run directly on those.
FYI, how it works now
We partition MyPy based on interpreter constraints, which allows for you to run over a codebase that might have some parts only Py2 and another part only Py3:
pants/src/python/pants/backend/python/typecheck/mypy/rules.py
Lines 60 to 65 in 9954e08
All the interesting work happens on partitions, in this rule:
pants/src/python/pants/backend/python/typecheck/mypy/rules.py
Lines 184 to 185 in 9954e08
We get all the source files for the partition here, which will continue to be the same so that we can include the files in our chroot:
pants/src/python/pants/backend/python/typecheck/mypy/rules.py
Lines 225 to 227 in 9954e08
What will change is this:
pants/src/python/pants/backend/python/typecheck/mypy/rules.py
Lines 278 to 282 in 9954e08
We won't run on all of
typechecked_sources
anymore, only what was specified. (FYI, see https://www.pantsbuild.org/docs/rules-api-file-system#core-abstractions-digest-and-snapshot for what the "snaphsot" part means.)"roots" vs. "closure"
Instead, we need to figure out which files were specified by the user. How? Looking back at the below definition, the
field_set_addresses
represent the roots, meaning what the user explicitly said to run on. Meanwhile,closure
represents the transitive closure: all roots + transitive deps.pants/src/python/pants/backend/python/typecheck/mypy/rules.py
Lines 60 to 65 in 9954e08
We want to operate on the roots, i.e. the
field_set_addresses
. We need to get the Python source files that belong specifically to those roots, whereas now we're getting it for the whole closure. We'll use the same approach as before, sayingGet(PythonSourceFiles, PythonSourceFilesRequest(..))
, which will run a rule that gets us the relevant Python source files; only, we'll run it over just the roots, rather than the whole closure.Part 1: refactor
First, there's a refactor to facilitate this change. Rather than storing on
MyPyPartition
the fieldfield_set_addresses: FrozenOrderedSet[Address]
, change it toroot_targets: FrozenOrderedSet[Target]
. We're going to need Target objects in a moment, and this is also a clearer name.Then, fix our rule that creates the partitions. Update to
combined_roots.update(transitive_targets.roots)
:pants/src/python/pants/backend/python/typecheck/mypy/rules.py
Lines 370 to 372 in 9954e08
You'll need to fix this too, use
tgt.address for tgt in partitions.root_targets
to convert fromTarget -> Address
:pants/src/python/pants/backend/python/typecheck/mypy/rules.py
Line 243 in 9954e08
Check that everything works by running
./pants typecheck src/python/pants/util/strutil.py
.Part 2: resolve the roots' files
See this:
pants/src/python/pants/backend/python/typecheck/mypy/rules.py
Lines 222 to 227 in 9954e08
First, that name
typechecked_sources_request
is confusing now. Really, it should probably be something likeclosure_sources_request
. We're not going to typecheck every one of those files anymore.Then, add something like
roots_sources_request = Get(PythonSourceFiles, PythonSourceFilesRequest(partition.root_targets))
. Feel free to use a better var name.Add to this
await MultiGet
, which is what will cause the engine to actually execute the relevant code:pants/src/python/pants/backend/python/typecheck/mypy/rules.py
Lines 264 to 276 in 9954e08
Finally, update this to point to your new
PythonSourceFiles
object with just the roots, rather than the original one with the closure.pants/src/python/pants/backend/python/typecheck/mypy/rules.py
Line 278 in 9954e08
Update this:
pants/src/python/pants/backend/python/typecheck/mypy/rules.py
Line 321 in 9954e08
That should be it! Check that things are working how you want. It can be helpful to add logging statements to debug. https://www.pantsbuild.org/docs/rules-api-tips#tip-add-logging
--
This function will still stay the same to normalize file names, but the docstring should be tweaked as we won't run over every file anymore:
pants/src/python/pants/backend/python/typecheck/mypy/rules.py
Lines 119 to 135 in 9954e08
Testing
You'll also want to check that
mypy/rules_integration_test.py
works correctly still. You may need to tweak some tests.It'd be helpful to add a simple test that ensures this behavior works correctly. We're going to be changing this MyPy code in the future and we don't want to regress. Look at how the other tests are doing it to see how to set up, and also refer to https://www.pantsbuild.org/docs/rules-api-testing.
The text was updated successfully, but these errors were encountered: