Skip to content

Commit 5316508

Browse files
committed
feat(mypy): centralize mypy.ini and update templates (#17523)
> [!note] > This is step one of a multi-step process. The work done here is outlined below. Additional steps (to be completed in other PRs) include: > * generate the **generated packages** > * generate and/or post process **hybrid packages** This work: * Adds a centralized `mypy.ini` file at the root of the repository. * Updates GAPIC generator templates to omit local `mypy.ini` and dynamically resolve the root config via a `MYPY_CONFIG_FILE` constant. > [!note] > Work on strictly handwritten libraries is outside the scope of this PR and can be found here: #17409 Partially resolves: #17322 🦕
1 parent 4095090 commit 5316508

22 files changed

Lines changed: 173 additions & 138 deletions

File tree

mypy.ini

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
[mypy]
2+
namespace_packages = True
3+
ignore_missing_imports = False
4+
5+
# Helps mypy navigate the "google" namespace more reliably in 3.10+
6+
explicit_package_bases = True
7+
8+
# Performance: reuse results from previous runs to speed up "nox"
9+
incremental = True
10+
11+
exclude = (?x)(
12+
(^|/)third_party/
13+
| (^|/)tests/unit/resources/
14+
| (^|/)tests/unit/gapic/
15+
)
16+
17+
18+
# ==============================================================================
19+
# GLOBAL THIRD-PARTY & SHARED LIBRARY IGNORES
20+
# ==============================================================================
21+
22+
[mypy-anywidget]
23+
ignore_missing_imports = True
24+
25+
[mypy-cloudpickle.*]
26+
ignore_missing_imports = True
27+
28+
[mypy-flask]
29+
ignore_missing_imports = True
30+
31+
[mypy-google.auth.*]
32+
ignore_missing_imports = True
33+
34+
[mypy-google.cloud.bigtable]
35+
ignore_missing_imports = True
36+
37+
[mypy-google.cloud.pubsub]
38+
ignore_missing_imports = True
39+
40+
[mypy-google.colab]
41+
ignore_missing_imports = True
42+
43+
[mypy-google.iam.*]
44+
ignore_missing_imports = True
45+
46+
[mypy-google.longrunning.*]
47+
ignore_missing_imports = True
48+
49+
[mypy-google.oauth2.*]
50+
ignore_missing_imports = True
51+
52+
[mypy-google.protobuf.*]
53+
ignore_missing_imports = True
54+
55+
[mypy-google.rpc.*]
56+
ignore_missing_imports = True
57+
58+
[mypy-google.type.*]
59+
ignore_missing_imports = True
60+
61+
[mypy-grpc.*]
62+
ignore_missing_imports = True
63+
64+
[mypy-ibis.*]
65+
ignore_missing_imports = True
66+
67+
[mypy-ipywidgets]
68+
ignore_missing_imports = True
69+
70+
[mypy-proto.*]
71+
ignore_missing_imports = True
72+
73+
[mypy-pyarrow.*]
74+
ignore_missing_imports = True
75+
76+
[mypy-pydata_google_auth]
77+
ignore_missing_imports = True
78+
79+
[mypy-pytest]
80+
ignore_missing_imports = True
81+
82+
[mypy-pytz]
83+
ignore_missing_imports = True
84+
85+
86+
# ==============================================================================
87+
# PACKAGE-SPECIFIC OVERRIDES & EXCEPTIONS
88+
# ==============================================================================
89+
90+
# --- google-cloud-bigtable ---
91+
[mypy-google.cloud.bigtable.*]
92+
ignore_errors = True
93+
94+
[mypy-google.cloud.bigtable.data.*]
95+
check_untyped_defs = True
96+
warn_unreachable = True
97+
disallow_any_generics = True
98+
ignore_errors = False
99+
100+

packages/gapic-generator/gapic/ads-templates/mypy.ini.j2

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/gapic-generator/gapic/ads-templates/noxfile.py.j2

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@
33
{% block content %}
44

55
import os
6+
import pathlib
67

78
import nox # type: ignore
89

10+
CURRENT_DIRECTORY = pathlib.Path(__file__).parent.absolute()
11+
# Search upwards to support running nox from both monorepo packages and integration test goldens.
12+
MYPY_CONFIG_FILE = next(
13+
(str(p / "mypy.ini") for p in CURRENT_DIRECTORY.parents if (p / "mypy.ini").exists()),
14+
str(CURRENT_DIRECTORY.parent.parent / "mypy.ini"),
15+
)
16+
917

1018
# TODO(https://github.com/googleapis/gapic-generator-python/issues/2450):
1119
# Add tests for Python 3.15 alpha1
@@ -44,6 +52,7 @@ def mypy(session):
4452
session.install('.')
4553
session.run(
4654
'mypy',
55+
f"--config-file={MYPY_CONFIG_FILE}",
4756
{% if api.naming.module_namespace %}
4857
'{{ api.naming.module_namespace[0] }}',
4958
{% else %}

packages/gapic-generator/gapic/templates/mypy.ini.j2

Lines changed: 0 additions & 15 deletions
This file was deleted.

packages/gapic-generator/gapic/templates/noxfile.py.j2

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ DEFAULT_PYTHON_VERSION = "3.14"
3838
PREVIEW_PYTHON_VERSION = "3.15"
3939

4040
CURRENT_DIRECTORY = pathlib.Path(__file__).parent.absolute()
41+
# Path to the centralized mypy configuration file at the repository root.
42+
# Search upwards to support running nox from both monorepo packages and integration test goldens.
43+
MYPY_CONFIG_FILE = next(
44+
(str(p / "mypy.ini") for p in CURRENT_DIRECTORY.parents if (p / "mypy.ini").exists()),
45+
str(CURRENT_DIRECTORY.parent.parent / "mypy.ini"),
46+
)
4147

4248
if (CURRENT_DIRECTORY / "testing").exists():
4349
LOWER_BOUND_CONSTRAINTS_FILE = (
@@ -99,6 +105,7 @@ def mypy(session):
99105
session.install(".")
100106
session.run(
101107
"mypy",
108+
f"--config-file={MYPY_CONFIG_FILE}",
102109
"-p",
103110
{% if api.naming.module_namespace %}
104111
"{{ api.naming.module_namespace[0] }}",

packages/gapic-generator/rules_python_gapic/test/integration_test.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ def _overwrite_golden_impl(ctx):
116116
# Filename pattern-based removal is needed to preserve the BUILD.bazel file.
117117
find tests/integration/goldens/{api_name}/ -name \\*.py -type f -delete
118118
find tests/integration/goldens/{api_name}/ -name \\*.json -type f -delete
119+
find tests/integration/goldens/{api_name}/ -name \\*.ini -type f -delete
119120
unzip -ao {goldens_output_zip} -d tests/integration/goldens/{api_name}
120121
""".format(
121122
goldens_output_zip = goldens_output_zip.path,

packages/gapic-generator/tests/integration/goldens/asset/mypy.ini

Lines changed: 0 additions & 15 deletions
This file was deleted.

packages/gapic-generator/tests/integration/goldens/asset/noxfile.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@
4545
PREVIEW_PYTHON_VERSION = "3.15"
4646

4747
CURRENT_DIRECTORY = pathlib.Path(__file__).parent.absolute()
48+
# Path to the centralized mypy configuration file at the repository root.
49+
# Search upwards to support running nox from both monorepo packages and integration test goldens.
50+
MYPY_CONFIG_FILE = next(
51+
(str(p / "mypy.ini") for p in CURRENT_DIRECTORY.parents if (p / "mypy.ini").exists()),
52+
str(CURRENT_DIRECTORY.parent.parent / "mypy.ini"),
53+
)
4854

4955
if (CURRENT_DIRECTORY / "testing").exists():
5056
LOWER_BOUND_CONSTRAINTS_FILE = (
@@ -106,6 +112,7 @@ def mypy(session):
106112
session.install(".")
107113
session.run(
108114
"mypy",
115+
f"--config-file={MYPY_CONFIG_FILE}",
109116
"-p",
110117
"google",
111118
"--check-untyped-defs",

packages/gapic-generator/tests/integration/goldens/credentials/mypy.ini

Lines changed: 0 additions & 15 deletions
This file was deleted.

packages/gapic-generator/tests/integration/goldens/credentials/noxfile.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@
4545
PREVIEW_PYTHON_VERSION = "3.15"
4646

4747
CURRENT_DIRECTORY = pathlib.Path(__file__).parent.absolute()
48+
# Path to the centralized mypy configuration file at the repository root.
49+
# Search upwards to support running nox from both monorepo packages and integration test goldens.
50+
MYPY_CONFIG_FILE = next(
51+
(str(p / "mypy.ini") for p in CURRENT_DIRECTORY.parents if (p / "mypy.ini").exists()),
52+
str(CURRENT_DIRECTORY.parent.parent / "mypy.ini"),
53+
)
4854

4955
if (CURRENT_DIRECTORY / "testing").exists():
5056
LOWER_BOUND_CONSTRAINTS_FILE = (
@@ -106,6 +112,7 @@ def mypy(session):
106112
session.install(".")
107113
session.run(
108114
"mypy",
115+
f"--config-file={MYPY_CONFIG_FILE}",
109116
"-p",
110117
"google",
111118
"--check-untyped-defs",

0 commit comments

Comments
 (0)