Skip to content

Commit aa9ab51

Browse files
committed
Fix per-file strict Optional interaction with default-None args
1 parent 005fd6b commit aa9ab51

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

mypy/fastparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ def do_func_def(self, n: Union[ast3.FunctionDef, ast3.AsyncFunctionDef],
390390
return func_def
391391

392392
def set_type_optional(self, type: Type, initializer: Expression) -> None:
393-
if self.options.no_implicit_optional or not experiments.STRICT_OPTIONAL:
393+
if self.options.no_implicit_optional:
394394
return
395395
# Indicate that type should be wrapped in an Optional if arg is initialized to None.
396396
optional = isinstance(initializer, NameExpr) and initializer.name == 'None'

mypy/fastparse2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ def visit_FunctionDef(self, n: ast27.FunctionDef) -> Statement:
364364
return func_def
365365

366366
def set_type_optional(self, type: Type, initializer: Expression) -> None:
367-
if self.options.no_implicit_optional or not experiments.STRICT_OPTIONAL:
367+
if self.options.no_implicit_optional:
368368
return
369369
# Indicate that type should be wrapped in an Optional if arg is initialized to None.
370370
optional = isinstance(initializer, NameExpr) and initializer.name == 'None'

mypy/typeanal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from mypy.exprtotype import expr_to_unanalyzed_type, TypeTranslationError
2727
from mypy.subtypes import is_subtype
2828
from mypy.plugin import Plugin, AnalyzerPluginInterface, AnalyzeTypeContext
29-
from mypy import nodes, messages
29+
from mypy import nodes, messages, experiments
3030

3131

3232
T = TypeVar('T')
@@ -144,7 +144,7 @@ def __init__(self,
144144
self.is_typeshed_stub = is_typeshed_stub
145145

146146
def visit_unbound_type(self, t: UnboundType) -> Type:
147-
if t.optional:
147+
if t.optional and experiments.STRICT_OPTIONAL:
148148
t.optional = False
149149
# We don't need to worry about double-wrapping Optionals or
150150
# wrapping Anys: Union simplification will take care of that.

test-data/unit/check-flags.test

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,23 @@ def f() -> None:
482482
x = [] # type: Union[List[Optional[str]], str]
483483
[builtins fixtures/list.pyi]
484484

485+
[case testPerFileStrictOptionalNoneArguments]
486+
# flags: --config-file tmp/mypy.ini
487+
import standard, optional
488+
489+
[file standard.py]
490+
def f(x: int = None) -> None: pass
491+
[file optional.py]
492+
def f(x: int = None) -> None: pass
493+
494+
[file mypy.ini]
495+
[[mypy]
496+
strict_optional = False
497+
[[mypy-optional]
498+
strict_optional = True
499+
500+
501+
485502
[case testDisallowImplicitTypesIgnoreMissingTypes]
486503
# flags: --ignore-missing-imports --disallow-any=unimported
487504
from missing import MyType

0 commit comments

Comments
 (0)