Skip to content

Commit 4aea3fd

Browse files
committed
Fix per-file strict Optional interaction with default-None args
1 parent a906d37 commit 4aea3fd

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

mypy/fastparse.py

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

394394
def set_type_optional(self, type: Type, initializer: Expression) -> None:
395-
if self.options.no_implicit_optional or not experiments.STRICT_OPTIONAL:
395+
if self.options.no_implicit_optional:
396396
return
397397
# Indicate that type should be wrapped in an Optional if arg is initialized to None.
398398
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
@@ -366,7 +366,7 @@ def visit_FunctionDef(self, n: ast27.FunctionDef) -> Statement:
366366
return func_def
367367

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

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)