Skip to content

Commit

Permalink
[internal] Add type asserts via mypy-typing-asserts (#14527)
Browse files Browse the repository at this point in the history
Add type asserts

[ci skip-rust]
  • Loading branch information
thejcannon authored Feb 19, 2022
1 parent 7546620 commit f9660a3
Show file tree
Hide file tree
Showing 8 changed files with 286 additions and 31 deletions.
84 changes: 84 additions & 0 deletions 3rdparty/python/lockfiles/mypy.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# This lockfile was autogenerated by Pants. To regenerate, run:
#
# build-support/bin/generate_all_lockfiles.sh
#
# --- BEGIN PANTS LOCKFILE METADATA: DO NOT EDIT OR REMOVE ---
# {
# "version": 2,
# "valid_for_interpreter_constraints": [
# "CPython<3.10,>=3.7"
# ],
# "generated_with_requirements": [
# "mypy-typing-asserts",
# "mypy==0.910"
# ]
# }
# --- END PANTS LOCKFILE METADATA ---

mypy-extensions==0.4.3; python_version >= "3.5" \
--hash=sha256:090fedd75945a69ae91ce1303b5824f428daf5a028d2f6ab8a299250a846f15d \
--hash=sha256:2d82818f5bb3e369420cb3c4060a7970edba416647068eb4c5343488a6c604a8
mypy-typing-asserts==0.1.1; python_version >= "3.7" and python_version < "4.0" \
--hash=sha256:917da6d572f187f72d6dfb4b1108c50a3d8edd8e5ccd1a0f0c576fe0a4819cda \
--hash=sha256:a93087a4242b660ae567463f93cab2673304bd0820172268f488248bf3b0fa40
mypy==0.910; python_version >= "3.5" \
--hash=sha256:a155d80ea6cee511a3694b108c4494a39f42de11ee4e61e72bc424c490e46457 \
--hash=sha256:b94e4b785e304a04ea0828759172a15add27088520dc7e49ceade7834275bedb \
--hash=sha256:088cd9c7904b4ad80bec811053272986611b84221835e079be5bcad029e79dd9 \
--hash=sha256:adaeee09bfde366d2c13fe6093a7df5df83c9a2ba98638c7d76b010694db760e \
--hash=sha256:ecd2c3fe726758037234c93df7e98deb257fd15c24c9180dacf1ef829da5f921 \
--hash=sha256:d9dd839eb0dc1bbe866a288ba3c1afc33a202015d2ad83b31e875b5905a079b6 \
--hash=sha256:3e382b29f8e0ccf19a2df2b29a167591245df90c0b5a2542249873b5c1d78212 \
--hash=sha256:53fd2eb27a8ee2892614370896956af2ff61254c275aaee4c230ae771cadd885 \
--hash=sha256:b6fb13123aeef4a3abbcfd7e71773ff3ff1526a7d3dc538f3929a49b42be03f0 \
--hash=sha256:e4dab234478e3bd3ce83bac4193b2ecd9cf94e720ddd95ce69840273bf44f6de \
--hash=sha256:7df1ead20c81371ccd6091fa3e2878559b5c4d4caadaf1a484cf88d93ca06703 \
--hash=sha256:0aadfb2d3935988ec3815952e44058a3100499f5be5b28c34ac9d79f002a4a9a \
--hash=sha256:ec4e0cd079db280b6bdabdc807047ff3e199f334050db5cbb91ba3e959a67504 \
--hash=sha256:119bed3832d961f3a880787bf621634ba042cb8dc850a7429f643508eeac97b9 \
--hash=sha256:866c41f28cee548475f146aa4d39a51cf3b6a84246969f3759cb3e9c742fc072 \
--hash=sha256:ceb6e0a6e27fb364fb3853389607cf7eb3a126ad335790fa1e14ed02fba50811 \
--hash=sha256:1a85e280d4d217150ce8cb1a6dddffd14e753a4e0c3cf90baabb32cefa41b59e \
--hash=sha256:42c266ced41b65ed40a282c575705325fa7991af370036d3f134518336636f5b \
--hash=sha256:3c4b8ca36877fc75339253721f69603a9c7fdb5d4d5a95a1a1b899d8b86a4de2 \
--hash=sha256:c0df2d30ed496a08de5daed2a9ea807d07c21ae0ab23acf541ab88c24b26ab97 \
--hash=sha256:c6c2602dffb74867498f86e6129fd52a2770c48b7cd3ece77ada4fa38f94eba8 \
--hash=sha256:ef565033fa5a958e62796867b1df10c40263ea9ded87164d67572834e57a174d \
--hash=sha256:704098302473cb31a218f1775a873b376b30b4c18229421e9e9dc8916fd16150
toml==0.10.2; python_version >= "3.5" and python_full_version < "3.0.0" or python_full_version >= "3.3.0" and python_version >= "3.5" \
--hash=sha256:806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b \
--hash=sha256:b3bda1d108d5dd99f4a20d24d9c348e91c4db7ab1b749200bded2f839ccbe68f
typed-ast==1.4.3; python_version < "3.8" and python_version >= "3.5" \
--hash=sha256:2068531575a125b87a41802130fa7e29f26c09a2833fea68d9a40cf33902eba6 \
--hash=sha256:c907f561b1e83e93fad565bac5ba9c22d96a54e7ea0267c708bffe863cbe4075 \
--hash=sha256:1b3ead4a96c9101bef08f9f7d1217c096f31667617b58de957f690c92378b528 \
--hash=sha256:dde816ca9dac1d9c01dd504ea5967821606f02e510438120091b84e852367428 \
--hash=sha256:777a26c84bea6cd934422ac2e3b78863a37017618b6e5c08f92ef69853e765d3 \
--hash=sha256:f8afcf15cc511ada719a88e013cec87c11aff7b91f019295eb4530f96fe5ef2f \
--hash=sha256:52b1eb8c83f178ab787f3a4283f68258525f8d70f778a2f6dd54d3b5e5fb4341 \
--hash=sha256:01ae5f73431d21eead5015997ab41afa53aa1fbe252f9da060be5dad2c730ace \
--hash=sha256:c190f0899e9f9f8b6b7863debfb739abcb21a5c054f911ca3596d12b8a4c4c7f \
--hash=sha256:398e44cd480f4d2b7ee8d98385ca104e35c81525dd98c519acff1b79bdaac363 \
--hash=sha256:bff6ad71c81b3bba8fa35f0f1921fb24ff4476235a6e94a26ada2e54370e6da7 \
--hash=sha256:0fb71b8c643187d7492c1f8352f2c15b4c4af3f6338f21681d3681b3dc31a266 \
--hash=sha256:760ad187b1041a154f0e4d0f6aae3e40fdb51d6de16e5c99aedadd9246450e9e \
--hash=sha256:5feca99c17af94057417d744607b82dd0a664fd5e4ca98061480fd8b14b18d04 \
--hash=sha256:95431a26309a21874005845c21118c83991c63ea800dd44843e42a916aec5899 \
--hash=sha256:aee0c1256be6c07bd3e1263ff920c325b59849dc95392a05f258bb9b259cf39c \
--hash=sha256:9ad2c92ec681e02baf81fdfa056fe0d818645efa9af1f1cd5fd6f1bd2bdfd805 \
--hash=sha256:b36b4f3920103a25e1d5d024d155c504080959582b928e91cb608a65c3a49e1a \
--hash=sha256:067a74454df670dcaa4e59349a2e5c81e567d8d65458d480a5b3dfecec08c5ff \
--hash=sha256:7538e495704e2ccda9b234b82423a4038f324f3a10c43bc088a1636180f11a41 \
--hash=sha256:af3d4a73793725138d6b334d9d247ce7e5f084d96284ed23f22ee626a7b88e39 \
--hash=sha256:f2362f3cb0f3172c42938946dbc5b7843c2a28aec307c49100c8b38764eb6927 \
--hash=sha256:dd4a21253f42b8d2b48410cb31fe501d32f8b9fbeb1f55063ad102fe9c425e40 \
--hash=sha256:f328adcfebed9f11301eaedfa48e15bdece9b519fb27e6a8c01aa52a17ec31b3 \
--hash=sha256:2c726c276d09fc5c414693a2de063f521052d9ea7c240ce553316f70656c84d4 \
--hash=sha256:cae53c389825d3b46fb37538441f75d6aecc4174f615d048321b716df2757fb0 \
--hash=sha256:b9574c6f03f685070d859e75c7f9eeca02d6933273b5e69572e5ff9d5e3931c3 \
--hash=sha256:209596a4ec71d990d71d5e0d312ac935d86930e6eecff6ccc7007fe54d703808 \
--hash=sha256:9c6d1a54552b5330bc657b7ef0eae25d00ba7ffe85d9ea8ae6540d2197a3788c \
--hash=sha256:fb1bbeac803adea29cedd70781399c99138358c26d05fcbd23c13016b7f5ec65
typing-extensions==4.1.1; python_version >= "3.6" \
--hash=sha256:21c85e0fe4b9a155d0799430b0ad741cdce7e359660ccbd8b530613e8df88ce2 \
--hash=sha256:1a9462dcc3347a79b1f1c0271fbe79e844580bb598bafa1ed208b94da3cdcd42
5 changes: 4 additions & 1 deletion 3rdparty/python/lockfiles/user_reqs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# "freezegun==1.1.0",
# "humbug==0.2.7",
# "ijson==3.1.4",
# "mypy-typing-asserts==0.1.1",
# "packaging==21.3",
# "pex==2.1.66",
# "psutil==5.9.0",
Expand Down Expand Up @@ -79,7 +80,6 @@ ijson==3.1.4 \
--hash=sha256:fa10a1d88473303ec97aae23169d77c5b92657b7fb189f9c584974c00a79f383 \
--hash=sha256:9a5bf5b9d8f2ceaca131ee21fc7875d0f34b95762f4f32e4d65109ca46472147 \
--hash=sha256:81cc8cee590c8a70cca3c9aefae06dd7cb8e9f75f3a7dc12b340c2e332d33a2a \
--hash=sha256:4ea5fc50ba158f72943d5174fbc29ebefe72a2adac051c814c87438dc475cf78 \
--hash=sha256:3b98861a4280cf09d267986cefa46c3bd80af887eae02aba07488d80eb798afa \
--hash=sha256:068c692efba9692406b86736dcc6803e4a0b6280d7f0b7534bff3faec677ff38 \
--hash=sha256:86884ac06ac69cea6d89ab7b84683b3b4159c4013e4a20276d3fc630fe9b7588 \
Expand Down Expand Up @@ -138,6 +138,9 @@ importlib-metadata==4.11.1; python_version < "3.8" and python_version >= "3.7" \
iniconfig==1.1.1; python_version >= "3.6" \
--hash=sha256:011e24c64b7f47f6ebd835bb12a743f2fbe9a26d4cecaa7f53bc4f35ee9da8b3 \
--hash=sha256:bc3af051d7d14b2ee5ef9969666def0cd1a000e121eaea580d4a313df4b37f32
mypy-typing-asserts==0.1.1; python_version >= "3.7" and python_version < "4.0" \
--hash=sha256:917da6d572f187f72d6dfb4b1108c50a3d8edd8e5ccd1a0f0c576fe0a4819cda \
--hash=sha256:a93087a4242b660ae567463f93cab2673304bd0820172268f488248bf3b0fa40
packaging==21.3; python_version >= "3.6" \
--hash=sha256:ef103e05f519cdc783ae24ea4e2e0f508a9c99b2d4969652eed6a2e1ea5bd522 \
--hash=sha256:dd47c42927d89ab911e606518907cc2d3a1f38bbd026385970643f9c5b8ecfeb
Expand Down
3 changes: 2 additions & 1 deletion 3rdparty/python/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ ijson==3.1.4
packaging==21.3
pex==2.1.66
psutil==5.9.0
pytest>=6.2.4,<8 # This should be compatible with pytest.py, although it can be looser so that we don't over-constrain pantsbuild.pants.testutil
pytest>=6.2.4,<8 # This should be compatible with pytest.py, although it can be looser so that we don't over-constrain pantsbuild.pants.testutil
python-lsp-jsonrpc==1.0.0
PyYAML>=6.0,<7.0
requests[security]>=2.25.1
Expand All @@ -29,3 +29,4 @@ types-requests==2.27.5
types-setuptools==57.4.7
types-toml==0.10.3
typing-extensions==4.0.1
mypy-typing-asserts==0.1.1
7 changes: 7 additions & 0 deletions pants.toml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ extra_env_vars = [
"RUST_BACKTRACE=1",
]

[mypy]
interpreter_constraints = [">=3.7,<3.10"]
extra_requirements.add = [
"mypy-typing-asserts",
]
lockfile = "3rdparty/python/lockfiles/mypy.txt"

[coverage-py]
interpreter_constraints = [">=3.7,<3.10"]

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ omit = ["src/python/pants/__init__.py"]
namespace_packages = true
explicit_package_bases = true
mypy_path = "src/python:tests/python:testprojects/src/python"
plugins = "mypy_typing_asserts.mypy_plugin"

no_implicit_optional = true
implicit_reexport = false
Expand Down
78 changes: 72 additions & 6 deletions src/python/pants/option/option_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,41 +337,107 @@ def _convert_(self, val: Any) -> dict[str, _ValueT]:
return cast("dict[str, _ValueT]", val)


class TargetOption(StrOption):
class TargetOption(_OptionBase[_PropType]):
"""A Pants Target option."""

option_type: Any = custom_types.target_option

@overload
def __new__(cls, *flag_names: str, default: str, help: str) -> TargetOption[str]:
...

@overload
def __new__(cls, *flag_names: str, help: str) -> TargetOption[str | None]:
...

def __new__(cls, *flag_names, default=None, help):
return super().__new__(cls, *flag_names, default=default, help=help)


class DirOption(StrOption):
class DirOption(_OptionBase[_PropType]):
"""A directory option."""

option_type: Any = custom_types.dir_option

@overload
def __new__(cls, *flag_names: str, default: str, help: str) -> DirOption[str]:
...

@overload
def __new__(cls, *flag_names: str, help: str) -> DirOption[str | None]:
...

class FileOption(StrOption):
def __new__(cls, *flag_names, default=None, help):
return super().__new__(cls, *flag_names, default=default, help=help)


class FileOption(_OptionBase[_PropType]):
"""A file option."""

option_type: Any = custom_types.file_option

@overload
def __new__(cls, *flag_names: str, default: str, help: str) -> FileOption[str]:
...

@overload
def __new__(cls, *flag_names: str, help: str) -> FileOption[str | None]:
...

def __new__(cls, *flag_names, default=None, help):
return super().__new__(cls, *flag_names, default=default, help=help)


class ShellStrOption(StrOption):
class ShellStrOption(_OptionBase[_PropType]):
"""A shell string option."""

option_type: Any = custom_types.shell_str

@overload
def __new__(cls, *flag_names: str, default: str, help: str) -> ShellStrOption[str]:
...

@overload
def __new__(cls, *flag_names: str, help: str) -> ShellStrOption[str | None]:
...

def __new__(cls, *flag_names, default=None, help):
return super().__new__(cls, *flag_names, default=default, help=help)


class WorkspacePathOption(StrOption):
class WorkspacePathOption(_OptionBase[_PropType]):
"""A workspace path option."""

option_type: Any = custom_types.workspace_path

@overload
def __new__(cls, *flag_names: str, default: str, help: str) -> WorkspacePathOption[str]:
...

@overload
def __new__(cls, *flag_names: str, help: str) -> WorkspacePathOption[str | None]:
...

class MemorySizeOption(IntOption):
def __new__(cls, *flag_names, default=None, help):
return super().__new__(cls, *flag_names, default=default, help=help)


class MemorySizeOption(_OptionBase[_PropType]):
"""A memory size option."""

option_type: Any = custom_types.memory_size

@overload
def __new__(cls, *flag_names: str, default: int, help: str) -> MemorySizeOption[int]:
...

@overload
def __new__(cls, *flag_names: str, help: str) -> MemorySizeOption[int | None]:
...

def __new__(cls, *flag_names, default=None, help):
return super().__new__(cls, *flag_names, default=default, help=help)


class StrListOption(_ListOptionBase[str]):
"""A homogenous list of string options."""
Expand Down
Loading

0 comments on commit f9660a3

Please sign in to comment.