From 58552e7ba1c22d7d4bf72da4c4db1ace9f969860 Mon Sep 17 00:00:00 2001 From: Peter Lamut Date: Mon, 27 Sep 2021 16:44:17 +0200 Subject: [PATCH] process: add type annotations check to CI runs (#499) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add pytype nox session * Disable false pytype positives in types.py * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot --- .gitignore | 1 + google/cloud/pubsub_v1/types.py | 8 +++-- noxfile.py | 12 +++++++ owlbot.py | 56 +++++++++++++++++++++++++++++++++ setup.cfg | 14 +++++++++ 5 files changed, 88 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index b4243ced7..99c3a1444 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,7 @@ pip-log.txt .nox .cache .pytest_cache +.pytype # Mac diff --git a/google/cloud/pubsub_v1/types.py b/google/cloud/pubsub_v1/types.py index 5fc7dd581..f8aa532a0 100644 --- a/google/cloud/pubsub_v1/types.py +++ b/google/cloud/pubsub_v1/types.py @@ -45,7 +45,7 @@ BatchSettings = collections.namedtuple( "BatchSettings", ["max_bytes", "max_latency", "max_messages"] ) -BatchSettings.__new__.__defaults__ = ( +BatchSettings.__new__.__defaults__ = ( # pytype: disable=attribute-error 1 * 1000 * 1000, # max_bytes: 1 MB 0.01, # max_latency: 10 ms 100, # max_messages: 100 @@ -78,11 +78,13 @@ class LimitExceededBehavior(str, enum.Enum): PublishFlowControl = collections.namedtuple( "PublishFlowControl", ["message_limit", "byte_limit", "limit_exceeded_behavior"] ) +# pytype: disable=attribute-error PublishFlowControl.__new__.__defaults__ = ( 10 * BatchSettings.__new__.__defaults__[2], # message limit 10 * BatchSettings.__new__.__defaults__[0], # byte limit LimitExceededBehavior.IGNORE, # desired behavior ) +# pytype: enable=attribute-error PublishFlowControl.__doc__ = "The client flow control settings for message publishing." PublishFlowControl.message_limit.__doc__ = ( "The maximum number of messages awaiting to be published." @@ -101,7 +103,7 @@ class LimitExceededBehavior(str, enum.Enum): PublisherOptions = collections.namedtuple( "PublisherOptions", ["enable_message_ordering", "flow_control", "retry", "timeout"] ) -PublisherOptions.__new__.__defaults__ = ( +PublisherOptions.__new__.__defaults__ = ( # pytype: disable=attribute-error False, # enable_message_ordering: False PublishFlowControl(), # default flow control settings gapic_v1.method.DEFAULT, # use default api_core value for retry @@ -138,7 +140,7 @@ class LimitExceededBehavior(str, enum.Enum): "max_duration_per_lease_extension", ], ) -FlowControl.__new__.__defaults__ = ( +FlowControl.__new__.__defaults__ = ( # pytype: disable=attribute-error 100 * 1024 * 1024, # max_bytes: 100mb 1000, # max_messages: 1000 1 * 60 * 60, # max_lease_duration: 1 hour. diff --git a/noxfile.py b/noxfile.py index 23c89a0ea..d36e59abf 100644 --- a/noxfile.py +++ b/noxfile.py @@ -27,6 +27,9 @@ BLACK_VERSION = "black==19.10b0" BLACK_PATHS = ["docs", "google", "tests", "noxfile.py", "setup.py"] +PYTYPE_VERSION = "pytype==2021.4.9" + + DEFAULT_PYTHON_VERSION = "3.8" SYSTEM_TEST_PYTHON_VERSIONS = ["3.8"] UNIT_TEST_PYTHON_VERSIONS = ["3.6", "3.7", "3.8", "3.9"] @@ -41,6 +44,7 @@ "lint", "lint_setup_py", "blacken", + "pytype", "docs", ] @@ -48,6 +52,14 @@ nox.options.error_on_missing_interpreters = True +@nox.session(python=DEFAULT_PYTHON_VERSION) +def pytype(session): + """Run type checks.""" + session.install("-e", ".[all]") + session.install(PYTYPE_VERSION) + session.run("pytype") + + @nox.session(python=DEFAULT_PYTHON_VERSION) def lint(session): """Run linters. diff --git a/owlbot.py b/owlbot.py index 60bb31941..ac57d3394 100644 --- a/owlbot.py +++ b/owlbot.py @@ -362,4 +362,60 @@ # ---------------------------------------------------------------------------- python.py_samples() +# ---------------------------------------------------------------------------- +# pytype-related changes +# ---------------------------------------------------------------------------- + +# Add .pytype to .gitignore +s.replace(".gitignore", r"\.pytest_cache", "\g<0>\n.pytype") + +# Add pytype config to setup.cfg +s.replace( + "setup.cfg", + r"universal = 1", + textwrap.dedent( + """ \g<0> + + [pytype] + python_version = 3.8 + inputs = + google/cloud/ + exclude = + tests/ + google/pubsub_v1/ # generated code + output = .pytype/ + disable = + # There's some issue with finding some pyi files, thus disabling. + # The issue https://github.com/google/pytype/issues/150 is closed, but the + # error still occurs for some reason. + pyi-error""" + ), +) + +# Add pytype session to noxfile.py +s.replace( + "noxfile.py", + r"BLACK_PATHS = \[.*?\]", + '\g<0>\n\nPYTYPE_VERSION = "pytype==2021.4.9"\n', +) +s.replace( + "noxfile.py", r'"blacken",', '\g<0>\n "pytype",', +) +s.replace( + "noxfile.py", + r"nox\.options\.error_on_missing_interpreters = True", + textwrap.dedent( + ''' \g<0> + + + @nox.session(python=DEFAULT_PYTHON_VERSION) + def pytype(session): + """Run type checks.""" + session.install("-e", ".[all]") + session.install(PYTYPE_VERSION) + session.run("pytype")''' + ), +) + + s.shell.run(["nox", "-s", "blacken"], hide_output=False) diff --git a/setup.cfg b/setup.cfg index c3a2b39f6..a79cb6a60 100644 --- a/setup.cfg +++ b/setup.cfg @@ -17,3 +17,17 @@ # Generated by synthtool. DO NOT EDIT! [bdist_wheel] universal = 1 + +[pytype] +python_version = 3.8 +inputs = + google/cloud/ +exclude = + tests/ + google/pubsub_v1/ # generated code +output = .pytype/ +disable = + # There's some issue with finding some pyi files, thus disabling. + # The issue https://github.com/google/pytype/issues/150 is closed, but the + # error still occurs for some reason. + pyi-error