Hey all :wave: We have a custom plugin repo that w...
# plugins
a
Hey all 👋 We have a custom plugin repo that was initially written to support pants 2.19, but now we want to add support for pants 2.24 and I'm seeing some weird
mypy
behavior when running
pants check ::
Copy code
Partition #1 - pants-2-19, ['CPython==3.9.*']:
<my_file.py>: error: Unexpected keyword argument "paths" for "Sandbox"; did you mean "path"?  [call-arg]
Found 1 error in 1 file (checked 1 source file)

Partition #2 - pants-2-24, ['CPython==3.9.*']:
<my_file.py>: error: Unexpected keyword argument "path" for "Sandbox"; did you mean "paths"?  [call-arg]
My
my_file.py
looks like:
Copy code
from pants.core.util_rules.adhoc_process_support import ExtraSandboxContents
from pants.version import Version, PANTS_SEMVER

class Sandbox(ExtraSandboxContents):

    @staticmethod
    def from_process(process: Process):
        env_with_out_path: dict[str, str] = dict()
        env_with_out_path.update(process.env)
        env_with_out_path.pop("PATH", None)

        if PANTS_SEMVER < Version("2.23.0"):
            return Sandbox(
                digest=process.input_digest,
                path=process.env.get("PATH", None),   <<< this is the prop when using pants<2.23.0
                immutable_input_digests=process.immutable_input_digests,
                append_only_caches=process.append_only_caches,
                extra_env=FrozenDict(env_with_out_path),
            )

        return Sandbox(
            digest=process.input_digest,
            paths=(process.env.get("PATH", None),),  <<< this is the new propery on pants>=2.23.0
            immutable_input_digests=process.immutable_input_digests,
            append_only_caches=process.append_only_caches,
            extra_env=FrozenDict(env_with_out_path),
        )
I had to add the
if
for the version because the
path
property of
Sandbox
was replaced with
paths
on pants 2.23 via https://github.com/pantsbuild/pants/commit/3b1ffb64bc2332e3925ffba387ce496143a4284d#diff-c440a5c58ef3255b75e7fb16e[…]7e2c3aba4567a95f44fa1edf8a4L139, but it looks like
mypy
is not being able to correctly run taking into consideration the pants version, as it fails on pants 2.19 with
paths
doesn't exist in
Sandbox
, and fails on pants 2.24 with
path
doesn't exist in
Sandbox
. I do have the
resolves
configured for each pants version, so that I can run lint, tests, check, etc, for both pants versions. Does anyone know how to correctly run
mypy
with multiple pants versions?
b
I think it'll be ~near impossible to get mypy to understand this sort of conditional control flow. That is, mypy would need to know that
PANTS_SEMVER
is connected to the API shape of
Sandbox
. So, I think a
# type: ignore[call-arg]
comment may be your best bet here: overrule mypy because you 'know more'
If this comes up a lot, you could package it into a dedicated function that has a consistent API and just internally manages the underlying differences.
a
I see, that makes sense. Thanks for the explanation! Will try the first approach of commenting to ignore that specific rule for now.