Skip to content

Commit

Permalink
process: add type annotations check to CI runs (#499)
Browse files Browse the repository at this point in the history
* 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 <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
plamut and gcf-owl-bot[bot] committed Sep 27, 2021
1 parent b72522a commit 58552e7
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -29,6 +29,7 @@ pip-log.txt
.nox
.cache
.pytest_cache
.pytype


# Mac
Expand Down
8 changes: 5 additions & 3 deletions google/cloud/pubsub_v1/types.py
Expand Up @@ -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
Expand Down Expand Up @@ -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."
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions noxfile.py
Expand Up @@ -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"]
Expand All @@ -41,13 +44,22 @@
"lint",
"lint_setup_py",
"blacken",
"pytype",
"docs",
]

# Error if a python version is missing
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.
Expand Down
56 changes: 56 additions & 0 deletions owlbot.py
Expand Up @@ -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)
14 changes: 14 additions & 0 deletions setup.cfg
Expand Up @@ -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

0 comments on commit 58552e7

Please sign in to comment.