Skip to content

Commit 3f50e3c

Browse files
authored
stubgen: add import for types in __exit__ method signature (#19120)
Fixes #17037 Add import for `types` in `__exit__` method signature
1 parent 9f53138 commit 3f50e3c

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

mypy/stubgen.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,11 @@ def _get_func_args(self, o: FuncDef, ctx: FunctionContext) -> list[ArgSig]:
633633
new_args = infer_method_arg_types(
634634
ctx.name, ctx.class_info.self_var, [arg.name for arg in args]
635635
)
636+
637+
if ctx.name == "__exit__":
638+
self.import_tracker.add_import("types")
639+
self.import_tracker.require_name("types")
640+
636641
if new_args is not None:
637642
args = new_args
638643

test-data/unit/stubgen.test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3777,6 +3777,8 @@ class MatchNames:
37773777
def __exit__(self, type, value, traceback): ...
37783778

37793779
[out]
3780+
import types
3781+
37803782
class MismatchNames:
37813783
def __exit__(self, tp: type[BaseException] | None, val: BaseException | None, tb: types.TracebackType | None) -> None: ...
37823784

0 commit comments

Comments
 (0)