From a1480dfc2eed5b6ba50e1646c5604aa6ec8a6842 Mon Sep 17 00:00:00 2001 From: KotlinIsland Date: Thu, 11 Aug 2022 11:38:01 +1000 Subject: [PATCH] infer-function-types: fix infer from defaults with inner functions --- CHANGELOG.md | 1 + mypy/semanal.py | 2 +- test-data/unit/check-based-infer-function-types.test | 6 ++++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d166ed926..0e0ae2509 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ ### Fixes - Handle positional only `/` parameters in overload implementation inference - Render inferred literal without `?` +- Fix infer from defaults for inner functions ## [1.5.0] ### Added diff --git a/mypy/semanal.py b/mypy/semanal.py index e6efcd872..eb90b7abc 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -3040,7 +3040,7 @@ def analyze_simple_literal_type( """Return builtins.int if rvalue is an int literal, etc. If this is a 'Final' context, we return "Literal[...]" instead.""" - if self.options.semantic_analysis_only or self.function_stack: + if self.options.semantic_analysis_only or self.function_stack and not do_bools: # Skip this if we're only doing the semantic analysis pass. # This is mostly to avoid breaking unit tests. # Also skip inside a function; this is to avoid confusing diff --git a/test-data/unit/check-based-infer-function-types.test b/test-data/unit/check-based-infer-function-types.test index 909847fe7..debc90889 100644 --- a/test-data/unit/check-based-infer-function-types.test +++ b/test-data/unit/check-based-infer-function-types.test @@ -468,3 +468,9 @@ def deco(func: T) -> T: ... def b(b=True) -> None: ... reveal_type(b) # N: Revealed type is "def (b: bool =) -> None" + + +[case testInferFromDefaultNested] +def f1(): + def f2(a=True): ... + reveal_type(f2) # N: Revealed type is "def (a: bool =) -> None"