Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add presubmit for mypy #517

Open
parthea opened this issue Jan 17, 2025 · 0 comments
Open

Add presubmit for mypy #517

parthea opened this issue Jan 17, 2025 · 0 comments
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: process A process-related concern. May include testing, release, or the like.

Comments

@parthea
Copy link
Contributor

parthea commented Jan 17, 2025

Add presubmit for mypy to avoid issues with type hints such as #489

As of writing, here is the output of mypy using the following nox session

@nox.session(python="3.10")
def mypy(session):
    """Run type-checking."""
    session.install(".", "mypy", "pytest")
    session.install(
         "types-pytz",
         "types-protobuf",
    )
    session.run("mypy", "proto", "tests")

partheniou@partheniou-vm-3:~/git/proto-plus-python$ nox -s mypy
nox > Running session mypy
nox > Creating virtual environment (virtualenv) using python3.10 in .nox/mypy
nox > python -m pip install . mypy pytest
nox > python -m pip install types-pytz types-protobuf
nox > mypy proto tests
proto/modules.py:19: error: First argument to namedtuple() should be "_ProtoModule", not "ProtoModule"  [name-match]
proto/modules.py:26: error: Incompatible default for argument "marshal" (default has type "None", argument has type "str")  [assignment]
proto/modules.py:26: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
proto/modules.py:26: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
proto/modules.py:26: error: Incompatible default for argument "manifest" (default has type "frozenset[Never]", argument has type "set[str]")  [assignment]
proto/marshal/rules/message.py:23: error: Incompatible default for argument "absent" (default has type "None", argument has type "bool")  [assignment]
proto/marshal/rules/message.py:23: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
proto/marshal/rules/message.py:23: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
proto/marshal/rules/message.py:25: error: "type" has no attribute "wrap"  [attr-defined]
proto/utils.py:20: error: Skipping analyzing "google._upb": module is installed, but missing library stubs or py.typed marker  [import-untyped]
proto/marshal/rules/stringy_numbers.py:30: error: Incompatible default for argument "absent" (default has type "None", argument has type "bool")  [assignment]
proto/marshal/rules/stringy_numbers.py:30: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
proto/marshal/rules/stringy_numbers.py:30: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
proto/marshal/rules/bytes.py:35: error: Incompatible default for argument "absent" (default has type "None", argument has type "bool")  [assignment]
proto/marshal/rules/bytes.py:35: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
proto/marshal/rules/bytes.py:35: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
proto/fields.py:37: error: Incompatible default for argument "oneof" (default has type "None", argument has type "str")  [assignment]
proto/fields.py:37: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
proto/fields.py:37: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
proto/fields.py:38: error: Incompatible default for argument "json_name" (default has type "None", argument has type "str")  [assignment]
proto/fields.py:38: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
proto/fields.py:38: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
proto/fields.py:122: error: Value of type "None" is not indexable  [index]
proto/fields.py:127: error: Value of type "None" is not indexable  [index]
proto/marshal/compat.py:27: error: Skipping analyzing "google._upb": module is installed, but missing library stubs or py.typed marker  [import-untyped]
proto/marshal/compat.py:32: error: Cannot find implementation or library stub for module named "google.protobuf.pyext"  [import-not-found]
proto/marshal/compat.py:32: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
proto/marshal/compat.py:50: error: Incompatible types in assignment (expression has type "tuple[type[RepeatedCompositeFieldContainer[Any]], Any]", variable has type "tuple[type[RepeatedCompositeFieldContainer[Any]]]")  [assignment]
proto/marshal/compat.py:51: error: Incompatible types in assignment (expression has type "tuple[type[RepeatedScalarFieldContainer[Any]], Any]", variable has type "tuple[type[RepeatedScalarFieldContainer[Any]]]")  [assignment]
proto/marshal/compat.py:54: error: Incompatible types in assignment (expression has type "tuple[type[MessageMap[Any, Any]], Any]", variable has type "tuple[type[MessageMap[Any, Any]]]")  [assignment]
proto/marshal/rules/enums.py:26: error: Incompatible default for argument "absent" (default has type "None", argument has type "bool")  [assignment]
proto/marshal/rules/enums.py:26: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
proto/marshal/rules/enums.py:26: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
proto/marshal/collections/repeated.py:75: error: Incompatible default for argument "key" (default has type "None", argument has type "str")  [assignment]
proto/marshal/collections/repeated.py:75: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
proto/marshal/collections/repeated.py:75: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
proto/_file_info.py:35: error: Need type annotation for "registry" (hint: "registry: dict[<type>, <type>] = ...")  [var-annotated]
proto/marshal/rules/wrappers.py:29: error: Incompatible default for argument "absent" (default has type "None", argument has type "bool")  [assignment]
proto/marshal/rules/wrappers.py:29: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
proto/marshal/rules/wrappers.py:29: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
proto/marshal/rules/wrappers.py:30: error: "WrapperRule" has no attribute "_proto_type"  [attr-defined]
proto/marshal/rules/struct.py:29: error: Incompatible default for argument "absent" (default has type "None", argument has type "bool")  [assignment]
proto/marshal/rules/struct.py:29: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
proto/marshal/rules/struct.py:29: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
proto/marshal/rules/struct.py:69: error: Argument "null_value" to "Value" has incompatible type "int"; expected "ValueType | None"  [arg-type]
proto/marshal/rules/struct.py:93: error: Incompatible default for argument "absent" (default has type "None", argument has type "bool")  [assignment]
proto/marshal/rules/struct.py:93: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
proto/marshal/rules/struct.py:93: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
proto/marshal/rules/struct.py:121: error: Incompatible default for argument "absent" (default has type "None", argument has type "bool")  [assignment]
proto/marshal/rules/struct.py:121: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
proto/marshal/rules/struct.py:121: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
proto/marshal/rules/field_mask.py:27: error: Incompatible default for argument "absent" (default has type "None", argument has type "bool")  [assignment]
proto/marshal/rules/field_mask.py:27: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
proto/marshal/rules/field_mask.py:27: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
proto/marshal/rules/dates.py:34: error: Incompatible default for argument "absent" (default has type "None", argument has type "bool")  [assignment]
proto/marshal/rules/dates.py:34: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
proto/marshal/rules/dates.py:34: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
proto/marshal/rules/dates.py:38: error: Incompatible return value type (got "None", expected "DatetimeWithNanoseconds")  [return-value]
proto/marshal/rules/dates.py:66: error: Incompatible default for argument "absent" (default has type "None", argument has type "bool")  [assignment]
proto/marshal/rules/dates.py:66: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
proto/marshal/rules/dates.py:66: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
proto/marshal/marshal.py:73: error: Incompatible default for argument "rule" (default has type "None", argument has type "Rule")  [assignment]
proto/marshal/marshal.py:73: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
proto/marshal/marshal.py:73: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
proto/marshal/marshal.py:178: error: Incompatible default for argument "absent" (default has type "None", argument has type "bool")  [assignment]
proto/marshal/marshal.py:178: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
proto/marshal/marshal.py:178: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
proto/marshal/marshal.py:258: error: Need type annotation for "_instances" (hint: "_instances: dict[<type>, <type>] = ...")  [var-annotated]
proto/marshal/marshal.py:290: error: Incompatible default for argument "absent" (default has type "None", argument has type "bool")  [assignment]
proto/marshal/marshal.py:290: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
proto/marshal/marshal.py:290: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
proto/message.py:452: error: Incompatible return value type (got "bool | None", expected "bool")  [return-value]
proto/message.py:510: error: Unexpected keyword argument "including_default_value_fields" for "MessageToJson"  [call-arg]
proto/message.py:597: error: Unexpected keyword argument "including_default_value_fields" for "MessageToDict"  [call-arg]
proto/message.py:597: error: Incompatible return value type (got "dict[str, Any]", expected "Message")  [return-value]
proto/message.py:610: error: Incompatible return value type (got "dict[str, Any]", expected "Message")  [return-value]
proto/message.py:966: error: Incompatible return value type (got "None", expected "type[Message]")  [return-value]
tests/test_marshal_types_struct.py:250: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
tests/test_marshal_types_struct.py:257: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
tests/test_fields_enum.py:343: error: Skipping analyzing "google.type": module is installed, but missing library stubs or py.typed marker  [import-untyped]
tests/test_fields_bytes.py:81: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
Found 41 errors in 17 files (checked 65 source files)
nox > Command mypy proto tests failed with exit code 1
nox > Session mypy failed.
proto/message.py:597: error: Incompatible return value type (got "dict[str, Any]", expected "Message")  [return-value]
proto/message.py:610: error: Incompatible return value type (got "dict[str, Any]", expected "Message")  [return-value]

should be fixed with #516

Once all errors are fixed, we can add the presubmit and prevent regressions

@parthea parthea added priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. type: process A process-related concern. May include testing, release, or the like. and removed type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: process A process-related concern. May include testing, release, or the like.
Projects
None yet
Development

No branches or pull requests

1 participant