Skip to content

Commit 55c8f4d

Browse files
oprypincopybara-github
authored andcommitted
Fix typechecking errors that appeared under mypy release 1.16
The following errors are now in GH Actions at HEAD: absl/flags/_helpers.py:108: error: Argument 1 to "get" of "dict" has incompatible type "Any | None"; expected "str" [arg-type] absl/flags/_helpers.py:111: error: Argument 2 to "_ModuleObjectAndName" has incompatible type "str | Any | None"; expected "str" [arg-type] absl/testing/absltest.py:2745: error: Incompatible types in assignment (expression has type "str", variable has type "_T") [assignment] To resolve these errors more constructively, I also change the interface of `_ModuleObjectAndName`: instead of a tuple of Optionals, it's an Optional tuple now. The whole file is private, so changing the API is OK. PiperOrigin-RevId: 766637390
1 parent aafb0d8 commit 55c8f4d

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

absl/flags/_defines.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,9 @@ def disclaim_key_flags() -> None:
363363
any more flags. This function will affect all FlagValues objects.
364364
"""
365365
globals_for_caller = sys._getframe(1).f_globals # pylint: disable=protected-access
366-
module, _ = _helpers.get_module_object_and_name(globals_for_caller)
367-
_helpers.disclaim_module_ids.add(id(module))
366+
module = _helpers.get_module_object_and_name(globals_for_caller)
367+
if module is not None:
368+
_helpers.disclaim_module_ids.add(id(module.module))
368369

369370

370371
@overload

absl/flags/_helpers.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ class _ModuleObjectAndName(NamedTuple):
8787
- module: object, module object.
8888
- module_name: str, module name.
8989
"""
90-
module: Optional[types.ModuleType]
90+
module: types.ModuleType
9191
module_name: str
9292

9393

9494
def get_module_object_and_name(
95-
globals_dict: Dict[str, Any]
96-
) -> _ModuleObjectAndName:
95+
globals_dict: Dict[str, Any],
96+
) -> Optional[_ModuleObjectAndName]:
9797
"""Returns the module that defines a global environment, and its name.
9898
9999
Args:
@@ -102,10 +102,13 @@ def get_module_object_and_name(
102102
103103
Returns:
104104
_ModuleObjectAndName - pair of module object & module name.
105-
Returns (None, None) if the module could not be identified.
105+
Returns None if the module could not be identified.
106106
"""
107-
name = globals_dict.get('__name__', None)
108-
module = sys.modules.get(name, None)
107+
try:
108+
name = globals_dict['__name__']
109+
module = sys.modules[name]
110+
except KeyError:
111+
return None
109112
# Pick a more informative name for the main module.
110113
return _ModuleObjectAndName(module,
111114
(sys.argv[0] if name == '__main__' else name))
@@ -127,9 +130,9 @@ def get_calling_module_object_and_name() -> _ModuleObjectAndName:
127130
# sys._getframe is the right thing to use here, as it's the best
128131
# way to walk up the call stack.
129132
globals_for_frame = sys._getframe(depth).f_globals # pylint: disable=protected-access
130-
module, module_name = get_module_object_and_name(globals_for_frame)
131-
if id(module) not in disclaim_module_ids and module_name is not None:
132-
return _ModuleObjectAndName(module, module_name)
133+
module = get_module_object_and_name(globals_for_frame)
134+
if module is not None and id(module.module) not in disclaim_module_ids:
135+
return module
133136
raise AssertionError('No module was found')
134137

135138

absl/testing/absltest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2746,7 +2746,7 @@ def _run_and_get_tests_result(
27462746
else:
27472747
xml_output_file = get_default_xml_output_filename()
27482748
if xml_output_file:
2749-
FLAGS.xml_output_file = xml_output_file
2749+
FLAGS.xml_output_file = xml_output_file # type: ignore[assignment]
27502750

27512751
xml_buffer = None
27522752
if xml_output_file:

0 commit comments

Comments
 (0)