Skip to content

Commit b2d3fb9

Browse files
authored
chore(handwritten): centralize mypy configuration and update handwritten package noxfiles (#17409)
* Relies on a centralized `mypy.ini` file at the root of the repository to consolidate all shared and package-specific mypy exceptions. * Deletes the individual `mypy.ini` files from handwritten libraries, establishing the root configuration as the source of truth. * Refactored their `noxfile.py` files to define `MYPY_CONFIG_FILE` dynamically and standardized their `session.run("mypy", ...)` invocations. > [!note] > Due to test failures outside the scope of this PR, neither google-cloud-crc32c nor bigframes are included in this PR. This [issue tracks those packages](#17606) to ensure their eventual completion > [!note] > bigquery and spanner were pulled out and placed in separate PRs due to excessive duration of their tests. PR #17640 and PR #17641 > [!note] > Work on generated libraries is outside the scope of this PR and can be found here: #17408 > [!note] > UPDATE (20260706): This PR is no longer blocked by #17607
1 parent 2a7d346 commit b2d3fb9

34 files changed

Lines changed: 489 additions & 92 deletions

File tree

.librarian/generator-input/client-post-processing/bigtable-integration.yaml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,16 @@ replacements:
419419
SYSTEM_TEST_EXTRAS_BY_PYTHON: Dict[str, List[str]] = {}
420420
421421
CURRENT_DIRECTORY = pathlib.Path(__file__).parent.absolute()
422+
# Path to the centralized mypy configuration file at the repository root.
423+
# Search upwards to support running nox from both monorepo packages and integration test goldens.
424+
MYPY_CONFIG_FILE = next(
425+
(
426+
str(p / "mypy.ini")
427+
for p in CURRENT_DIRECTORY.parents
428+
if (p / "mypy.ini").exists()
429+
),
430+
str(CURRENT_DIRECTORY.parent.parent / "mypy.ini"),
431+
)
422432
423433
# 'docfx' is excluded since it only needs to run in 'docs-presubmit'
424434
nox.options.sessions = [
@@ -528,7 +538,12 @@ replacements:
528538
"types-requests",
529539
)
530540
session.install("google-cloud-testutils")
531-
session.run("mypy", "-p", "google.cloud.bigtable.data")
541+
session.run(
542+
"mypy",
543+
f"--config-file={MYPY_CONFIG_FILE}",
544+
"-p",
545+
"google.cloud.bigtable.data",
546+
)
532547
533548
534549
@nox.session(python=DEFAULT_PYTHON_VERSION)

.librarian/generator-input/client-post-processing/logging-integration.yaml

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -328,14 +328,14 @@ replacements:
328328
-------------------
329329
.. toctree::
330330
:maxdepth: 2
331-
331+
332332
usage
333-
333+
334334
Documentation
335335
-------------------
336336
.. toctree::
337337
:maxdepth: 3
338-
338+
339339
client
340340
logger
341341
entries
@@ -344,20 +344,20 @@ replacements:
344344
sink
345345
handlers
346346
transport
347-
348-
347+
348+
349349
Migration Guides
350350
----------------
351-
351+
352352
See the guide below for instructions on migrating between major releases of this library.
353-
353+
354354
.. toctree::
355355
:maxdepth: 2
356-
356+
357357
UPGRADING
358358
count: 1
359359
- paths: [
360-
"packages/google-cloud-logging/google/cloud/logging/__init__.py",
360+
"packages/google-cloud-logging/google/cloud/logging/__init__.py",
361361
]
362362
before: |
363363
from google.cloud.logging import gapic_version as package_version
@@ -463,6 +463,24 @@ replacements:
463463
"django",
464464
]
465465
count: 1
466+
- paths: [
467+
packages/google-cloud-logging/noxfile.py
468+
]
469+
before: |
470+
CURRENT_DIRECTORY = pathlib.Path\(__file__\).parent.absolute\(\)
471+
after: |
472+
CURRENT_DIRECTORY = pathlib.Path(__file__).parent.absolute()
473+
# Path to the centralized mypy configuration file at the repository root.
474+
# Search upwards to support running nox from both monorepo packages and integration test goldens.
475+
MYPY_CONFIG_FILE = next(
476+
(
477+
str(p / "mypy.ini")
478+
for p in CURRENT_DIRECTORY.parents
479+
if (p / "mypy.ini").exists()
480+
),
481+
str(CURRENT_DIRECTORY.parent.parent / "mypy.ini"),
482+
)
483+
count: 1
466484
- paths: [
467485
packages/google-cloud-logging/noxfile.py
468486
]
@@ -478,4 +496,19 @@ replacements:
478496
# TODO(https://github.com/googleapis/google-cloud-python/issues/13362):
479497
# Enable mypy once this repo has been updated for mypy evaluation.
480498
session.skip("Skip mypy since this library is not yet updated for mypy evaluation")
499+
500+
session.install("-e", ".")
501+
session.install(
502+
"mypy",
503+
"types-setuptools",
504+
"types-protobuf",
505+
"types-requests",
506+
)
507+
session.run(
508+
"mypy",
509+
f"--config-file={MYPY_CONFIG_FILE}",
510+
"-p",
511+
"google",
512+
*session.posargs,
513+
)
481514
count: 1

.librarian/generator-input/client-post-processing/pubsub-integration.yaml

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
replacements:
2-
- paths:
2+
- paths:
33
- "packages/google-cloud-pubsub/google/pubsub_v1/services/**/*client.py"
44
before: 'instead\.\n(?:[ \t]*\n)*([ \t]+)DEFAULT_ENDPOINT = "pubsub\.googleapis\.com"'
55
after: |-
@@ -13,7 +13,7 @@ replacements:
1313
\g<1>DEFAULT_ENDPOINT = "pubsub.googleapis.com"
1414
count: 3
1515

16-
- paths:
16+
- paths:
1717
- "packages/google-cloud-pubsub/google/pubsub_v1/services/*/client.py"
1818
before: 'import json\n(?:[ \t]*\n)*import logging as std_logging'
1919
after: |-
@@ -23,7 +23,7 @@ replacements:
2323
import logging as std_logging
2424
count: 3
2525

26-
- paths:
26+
- paths:
2727
- "packages/google-cloud-pubsub/google/pubsub_v1/services/*/client.py"
2828
before: 'class\n(?:[ \t]*\n)*([ \t]+)self\._transport = transport_init\('
2929
after: |-
@@ -38,7 +38,7 @@ replacements:
3838
\g<1>self._transport = transport_init(
3939
count: 3
4040

41-
- paths:
41+
- paths:
4242
- "packages/google-cloud-pubsub/google/pubsub_v1/services/*/transports/grpc*.py"
4343
- "packages/google-cloud-pubsub/tests/unit/gapic/pubsub_v1/*.py"
4444
before: '\("grpc\.max_receive_message_length", -1\),\n(?:[ \t]*\n)*([ \t]+)\]'
@@ -49,19 +49,19 @@ replacements:
4949
\g<1>]
5050
count: 21
5151

52-
- paths:
52+
- paths:
5353
- "packages/google-cloud-pubsub/google/pubsub_v1/services/publisher/transports/base.py"
5454
before: 'deadline=60\.0,'
5555
after: 'deadline=600.0,'
5656
count: 9
5757

58-
- paths:
58+
- paths:
5959
- "packages/google-cloud-pubsub/google/pubsub_v1/services/**/*.py"
6060
before: 'gapic_version=package_version\.__version__'
6161
after: 'client_library_version=package_version.__version__'
6262
count: 9
6363

64-
- paths:
64+
- paths:
6565
- "packages/google-cloud-pubsub/google/pubsub_v1/services/subscriber/client.py"
6666
before: 'import logging as std_logging\n(?:[ \t]*\n)*import os'
6767
after: |-
@@ -71,7 +71,7 @@ replacements:
7171
import os
7272
count: 1
7373

74-
- paths:
74+
- paths:
7575
- "packages/google-cloud-pubsub/google/pubsub_v1/services/subscriber/async_client.py"
7676
before: 'import logging as std_logging\n(?:[ \t]*\n)*import re'
7777
after: |-
@@ -80,7 +80,7 @@ replacements:
8080
import re
8181
count: 1
8282

83-
- paths:
83+
- paths:
8484
- "packages/google-cloud-pubsub/google/pubsub_v1/services/subscriber/client.py"
8585
- "packages/google-cloud-pubsub/google/pubsub_v1/services/subscriber/async_client.py"
8686
before: 'request\.max_messages = max_messages\n(?:[ \t]*\n)*([ \t]+)# Wrap the RPC method'
@@ -96,7 +96,7 @@ replacements:
9696
\g<1># Wrap the RPC method
9797
count: 2
9898

99-
- paths:
99+
- paths:
100100
- "packages/google-cloud-pubsub/google/pubsub_v1/services/subscriber/client.py"
101101
before: '"""\n(?:[ \t]*\n)*([ \t]+)# Wrap the RPC method; this adds retry and timeout information,\n(?:[ \t]*\n)*[ \t]+# and friendly error handling\.\n(?:[ \t]*\n)*[ \t]+rpc = self\._transport\._wrapped_methods\[self\._transport\.streaming_pull\]'
102102
after: |-
@@ -403,6 +403,16 @@ replacements:
403403
before: 'nox\.options\.sessions = \[\n(?:[ \t]+)"unit",\n(?:[ \t]+)"system",\n(?:[ \t]+)"cover",\n(?:[ \t]+)"lint",\n(?:[ \t]+)"lint_setup_py",\n(?:[ \t]+)"blacken",\n(?:[ \t]+)"docs",\n\]'
404404
after: |-
405405
CURRENT_DIRECTORY = pathlib.Path(__file__).parent.absolute()
406+
# Path to the centralized mypy configuration file at the repository root.
407+
# Search upwards to support running nox from both monorepo packages and integration test goldens.
408+
MYPY_CONFIG_FILE = next(
409+
(
410+
str(p / "mypy.ini")
411+
for p in CURRENT_DIRECTORY.parents
412+
if (p / "mypy.ini").exists()
413+
),
414+
str(CURRENT_DIRECTORY.parent.parent / "mypy.ini"),
415+
)
406416
407417
nox.options.sessions = [
408418
"unit",
@@ -450,7 +460,7 @@ replacements:
450460
# mypy checks yet.
451461
# https://github.com/googleapis/gapic-generator-python/issues/1092
452462
# TODO: Re-enable mypy checks once we merge, since incremental checks are failing due to protobuf upgrade
453-
# session.run("mypy", "-p", "google.cloud", "--exclude", "google/pubsub_v1/")
463+
# session.run("mypy", f"--config-file={MYPY_CONFIG_FILE}", "-p", "google.cloud", "--exclude", "google/pubsub_v1/")
454464
455465
456466
@nox.session(python=DEFAULT_PYTHON_VERSION)

.librarian/generator-input/client-post-processing/storage-integration.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
description: Restore handwritten __init__.py for google-cloud-storage
22
replacements:
3+
- paths: [
4+
packages/google-cloud-storage/noxfile.py,
5+
]
6+
before: |
7+
CURRENT_DIRECTORY = pathlib.Path\(__file__\).parent.absolute\(\)
8+
after: |
9+
CURRENT_DIRECTORY = pathlib.Path(__file__).parent.absolute()
10+
# Path to the centralized mypy configuration file at the repository root.
11+
# Search upwards to support running nox from both monorepo packages and integration test goldens.
12+
MYPY_CONFIG_FILE = next(
13+
(
14+
str(p / "mypy.ini")
15+
for p in CURRENT_DIRECTORY.parents
16+
if (p / "mypy.ini").exists()
17+
),
18+
str(CURRENT_DIRECTORY.parent.parent / "mypy.ini"),
19+
)
20+
count: 1
321
- paths: [
422
packages/google-cloud-storage/noxfile.py,
523
]
@@ -804,4 +822,19 @@ replacements:
804822
# TODO(https://github.com/googleapis/google-cloud-python/issues/13362):
805823
# Enable mypy once this repo has been updated for mypy evaluation.
806824
session.skip("Skip mypy since this library is not yet updated for mypy evaluation")
825+
826+
session.install("-e", ".")
827+
session.install(
828+
"mypy",
829+
"types-setuptools",
830+
"types-protobuf",
831+
"types-requests",
832+
)
833+
session.run(
834+
"mypy",
835+
f"--config-file={MYPY_CONFIG_FILE}",
836+
"-p",
837+
"google",
838+
*session.posargs,
839+
)
807840
count: 1

mypy.ini

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,31 @@ ignore_missing_imports = True
2727
[mypy-flask]
2828
ignore_missing_imports = True
2929

30-
[mypy-google.api.*]
30+
[mypy-google.api,google.api.*]
3131
ignore_missing_imports = True
3232

3333
[mypy-google.colab]
3434
ignore_missing_imports = True
3535

36-
[mypy-google.iam.*]
36+
[mypy-google.iam,google.iam.*]
3737
ignore_missing_imports = True
3838

39-
[mypy-google.longrunning.*]
39+
[mypy-google.longrunning,google.longrunning.*]
4040
ignore_missing_imports = True
4141

42-
[mypy-google.oauth2.*]
42+
[mypy-google.oauth2,google.oauth2.*]
4343
ignore_missing_imports = True
4444

45-
[mypy-google.protobuf.*]
45+
[mypy-google.protobuf,google.protobuf.*]
4646
ignore_missing_imports = True
4747

48-
[mypy-google.rpc.*]
48+
[mypy-google.rpc,google.rpc.*]
4949
ignore_missing_imports = True
5050

51-
[mypy-google.type.*]
51+
[mypy-google.type,google.type.*]
5252
ignore_missing_imports = True
5353

54-
[mypy-grpc.*]
54+
[mypy-grpc,grpc.*]
5555
ignore_missing_imports = True
5656

5757
[mypy-grpc_status]
@@ -63,7 +63,7 @@ ignore_missing_imports = True
6363
[mypy-ipywidgets]
6464
ignore_missing_imports = True
6565

66-
[mypy-proto.*]
66+
[mypy-proto,proto.*]
6767
ignore_missing_imports = True
6868

6969
[mypy-pyarrow.*]
@@ -95,9 +95,11 @@ ignore_missing_imports = True
9595
ignore_missing_imports = True
9696

9797
[mypy-google.cloud.bigtable.*]
98+
# Broadly ignore errors for the older, legacy/handwritten parts of the library
9899
ignore_errors = True
99100

100101
[mypy-google.cloud.bigtable.data.*]
102+
# Stricter rules for the newer parts of the library
101103
check_untyped_defs = True
102104
warn_unreachable = True
103105
disallow_any_generics = True

packages/bigquery-magics/mypy.ini

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

packages/bigquery-magics/noxfile.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,17 @@
112112
}
113113

114114
CURRENT_DIRECTORY = pathlib.Path(__file__).parent.absolute()
115+
# Path to the centralized mypy configuration file at the repository root.
116+
# Search upwards to support running nox from both monorepo packages and integration test goldens.
117+
MYPY_CONFIG_FILE = next(
118+
(
119+
str(p / "mypy.ini")
120+
for p in CURRENT_DIRECTORY.parents
121+
if (p / "mypy.ini").exists()
122+
),
123+
str(CURRENT_DIRECTORY.parent.parent / "mypy.ini"),
124+
)
125+
115126

116127
nox.options.sessions = [
117128
"unit",
@@ -546,3 +557,18 @@ def mypy(session):
546557
# TODO(https://github.com/googleapis/google-cloud-python/issues/16014):
547558
# Add mypy tests
548559
session.skip("mypy tests are not yet supported")
560+
561+
session.install("-e", ".")
562+
session.install(
563+
"mypy",
564+
"types-setuptools",
565+
"types-protobuf",
566+
"types-requests",
567+
)
568+
session.run(
569+
"mypy",
570+
f"--config-file={MYPY_CONFIG_FILE}",
571+
"-p",
572+
"bigquery_magics",
573+
*session.posargs,
574+
)

0 commit comments

Comments
 (0)