Skip to content

Commit 5146ae2

Browse files
committed
Add --no-implicit-optional flag
1 parent 6ddd689 commit 5146ae2

File tree

6 files changed

+18
-12
lines changed

6 files changed

+18
-12
lines changed

docs/source/config_file.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ overridden by the pattern sections matching the module name.
172172
- ``strict_boolean`` (Boolean, default False) makes using non-boolean
173173
expressions in conditions an error.
174174

175+
- ``no_implicit_optional`` (Boolean, default false) changes the treatment of
176+
arguments with a default value of None by not implicitly making their type Optional
175177

176178
Example
177179
*******

mypy/fastparse.py

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

385385
def set_type_optional(self, type: Type, initializer: Expression) -> None:
386-
if not experiments.STRICT_OPTIONAL:
386+
if self.options.no_implicit_optional or not experiments.STRICT_OPTIONAL:
387387
return
388388
# Indicate that type should be wrapped in an Optional if arg is initialized to None.
389389
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 not experiments.STRICT_OPTIONAL:
367+
if self.options.no_implicit_optional or not experiments.STRICT_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/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ def add_invertible_flag(flag: str,
236236
add_invertible_flag('--show-error-context', default=False,
237237
dest='show_error_context',
238238
help='Precede errors with "note:" messages explaining context')
239+
add_invertible_flag('--no-implicit-optional', default=False, strict_flag=True,
240+
help="don't assume arguments with default values of None are Optional")
239241
parser.add_argument('-i', '--incremental', action='store_true',
240242
help="enable module cache")
241243
parser.add_argument('--quick-and-dirty', action='store_true',

mypy/options.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class Options:
2929
"warn_return_any",
3030
"ignore_errors",
3131
"strict_boolean",
32+
"no_implicit_optional",
3233
}
3334

3435
OPTIONS_AFFECTING_CACHE = PER_MODULE_OPTIONS | {"strict_optional", "quick_and_dirty"}
@@ -92,6 +93,9 @@ def __init__(self) -> None:
9293
# Alternate way to show/hide strict-None-checking related errors
9394
self.show_none_errors = True
9495

96+
# Don't assume arguments with default values of None are Optional
97+
self.no_implicit_optional = False
98+
9599
# Use script name instead of __main__
96100
self.scripts_are_modules = False
97101

test-data/unit/check-optional.test

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,10 @@ def f(x: int = None) -> None:
125125
f(None)
126126
[out]
127127

128-
[case testInferOptionalFromDefaultNoneWithFastParser]
129-
130-
def f(x: int = None) -> None:
131-
x + 1 # E: Unsupported left operand type for + (some union)
132-
f(None)
128+
[case testNoInferOptionalFromDefaultNone]
129+
# flags: --no-implicit-optional
130+
def f(x: int = None) -> None: # E: Incompatible types in assignment (expression has type None, variable has type "int")
131+
pass
133132
[out]
134133

135134
[case testInferOptionalFromDefaultNoneComment]
@@ -139,12 +138,11 @@ def f(x=None):
139138
f(None)
140139
[out]
141140

142-
[case testInferOptionalFromDefaultNoneCommentWithFastParser]
143-
144-
def f(x=None):
141+
[case testNoInferOptionalFromDefaultNoneComment]
142+
# flags: --no-implicit-optional
143+
def f(x=None): # E: Incompatible types in assignment (expression has type None, variable has type "int")
145144
# type: (int) -> None
146-
x + 1 # E: Unsupported left operand type for + (some union)
147-
f(None)
145+
pass
148146
[out]
149147

150148
[case testInferOptionalType]

0 commit comments

Comments
 (0)