Skip to content

Use --no-implicit-optional by default #13401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ def do_func_def(
return retval

def set_type_optional(self, type: Optional[Type], initializer: Optional[Expression]) -> None:
if self.options.no_implicit_optional:
if not self.options.implicit_optional:
return
# Indicate that type should be wrapped in an Optional if arg is initialized to None.
optional = isinstance(initializer, NameExpr) and initializer.name == "None"
Expand Down
5 changes: 2 additions & 3 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,10 +719,9 @@ def add_invertible_flag(
"https://mypy.readthedocs.io/en/stable/kinds_of_types.html#no-strict-optional",
)
add_invertible_flag(
"--no-implicit-optional",
"--implicit-optional",
default=False,
strict_flag=True,
help="Don't assume arguments with default values of None are Optional",
help="Assume arguments with default values of None are Optional",
group=none_group,
)
none_group.add_argument("--strict-optional", action="store_true", help=argparse.SUPPRESS)
Expand Down
10 changes: 5 additions & 5 deletions mypy/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ class BuildType:
"disallow_untyped_calls",
"disallow_untyped_decorators",
"disallow_untyped_defs",
"follow_imports",
"follow_imports_for_stubs",
"follow_imports",
"ignore_errors",
"ignore_missing_imports",
"implicit_optional",
"implicit_reexport",
"local_partial_types",
"mypyc",
"no_implicit_optional",
"show_none_errors",
"strict_concatenate",
"strict_equality",
"strict_optional",
"strict_optional_whitelist",
"strict_optional",
"warn_no_return",
"warn_return_any",
"warn_unreachable",
Expand Down Expand Up @@ -165,8 +165,8 @@ def __init__(self) -> None:
# Alternate way to show/hide strict-None-checking related errors
self.show_none_errors = True

# Don't assume arguments with default values of None are Optional
self.no_implicit_optional = False
# Assume arguments with default values of None are Optional
self.implicit_optional = False

# Don't re-export names unless they are imported with `from ... as ...`
self.implicit_reexport = True
Expand Down
2 changes: 2 additions & 0 deletions test-data/unit/check-flags.test
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,7 @@ standard.f(None)
[file mypy.ini]
\[mypy]
strict_optional = False
implicit_optional = true
\[mypy-optional]
strict_optional = True

Expand All @@ -846,6 +847,7 @@ standard.f(None)
[file pyproject.toml]
\[tool.mypy]
strict_optional = false
implicit_optional = true
\[[tool.mypy.overrides]]
module = 'optional'
strict_optional = true
Expand Down
1 change: 1 addition & 0 deletions test-data/unit/check-kwargs.test
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class A: pass
class B: pass

[case testOneOfSeveralOptionalKeywordArguments]
# flags: --implicit-optional
import typing
def f(a: 'A' = None, b: 'B' = None, c: 'C' = None) -> None: pass
f(a=A())
Expand Down
2 changes: 2 additions & 0 deletions test-data/unit/check-optional.test
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def f(x: None) -> None: pass
f(None)

[case testInferOptionalFromDefaultNone]
# flags: --implicit-optional
def f(x: int = None) -> None:
x + 1 # E: Unsupported left operand type for + ("None") \
# N: Left operand is of type "Optional[int]"
Expand All @@ -140,6 +141,7 @@ def f(x: int = None) -> None: # E: Incompatible default for argument "x" (defau
[out]

[case testInferOptionalFromDefaultNoneComment]
# flags: --implicit-optional
def f(x=None):
# type: (int) -> None
x + 1 # E: Unsupported left operand type for + ("None") \
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/fine-grained.test
Original file line number Diff line number Diff line change
Expand Up @@ -7852,7 +7852,7 @@ class Foo(a.I):
==

[case testImplicitOptionalRefresh1]
# flags: --strict-optional
# flags: --strict-optional --implicit-optional
from x import f
def foo(x: int = None) -> None:
f()
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/fixtures/tuple.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Builtins stub used in tuple-related test cases.

from typing import Iterable, Iterator, TypeVar, Generic, Sequence, Any, overload, Tuple, Type
from typing import Iterable, Iterator, TypeVar, Generic, Sequence, Optional, overload, Tuple, Type

T = TypeVar("T")
Tco = TypeVar('Tco', covariant=True)
Expand Down Expand Up @@ -46,6 +46,6 @@ class list(Sequence[T], Generic[T]):

def isinstance(x: object, t: type) -> bool: pass

def sum(iterable: Iterable[T], start: T = None) -> T: pass
def sum(iterable: Iterable[T], start: Optional[T] = None) -> T: pass

class BaseException: pass