forked from cython/cython
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid useless runtime type checks when coercing a None default argume…
…nt to a Python type argument. For Python int (which we turn into a plain Python object type internally to avoid Py2 int/long issues), we previously generated a type check which made the None default argument much more complex than it was. Closes cython#5643
- Loading branch information
Showing
2 changed files
with
95 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# mode: run | ||
# tag: pep484, warnings, pure3.6 | ||
# ticket: 5643 | ||
# cython: language_level=3 | ||
|
||
try: | ||
from typing import Optional | ||
except ImportError: | ||
pass | ||
|
||
|
||
# no crash | ||
def gh5643_optional(a: Optional[int] = None): | ||
""" | ||
>>> gh5643_optional() | ||
True | ||
>>> gh5643_optional(1) | ||
False | ||
""" | ||
return a is None | ||
|
||
|
||
# no crash | ||
def gh5643_int_untyped(a: int = 1, b = None): | ||
""" | ||
>>> gh5643_int_untyped(2) | ||
(False, True) | ||
>>> gh5643_int_untyped(2, None) | ||
(False, True) | ||
>>> gh5643_int_untyped(1, 3) | ||
(True, False) | ||
""" | ||
return a == 1, b is None | ||
|
||
|
||
# used to crash | ||
def gh5643_int_int_none(a: int = 1, b: int = None): # should warn about missing "Optional[]" | ||
""" | ||
>>> gh5643_int_int_none() | ||
(True, True) | ||
>>> gh5643_int_int_none(2, 3) | ||
(False, False) | ||
""" | ||
return a == 1, b is None | ||
|
||
|
||
def gh5643_int_int_integer(a: int = 1, b: int = 3): | ||
""" | ||
>>> gh5643_int_int_integer() | ||
(True, True) | ||
>>> gh5643_int_int_integer(2, 3) | ||
(False, True) | ||
""" | ||
return a == 1, b == 3 | ||
|
||
|
||
# used to crash | ||
def gh5643_int_optional_none(a: int = 1, b: Optional[int] = None): | ||
""" | ||
>>> gh5643_int_optional_none() | ||
(True, True) | ||
>>> gh5643_int_optional_none(2) | ||
(False, True) | ||
>>> gh5643_int_optional_none(2, 3) | ||
(False, False) | ||
""" | ||
return a == 1, b is None | ||
|
||
|
||
def gh5643_int_optional_integer(a: int = 1, b: Optional[int] = 2): | ||
""" | ||
>>> gh5643_int_optional_integer() | ||
(True, True) | ||
>>> gh5643_int_optional_integer(2) | ||
(False, True) | ||
>>> gh5643_int_optional_integer(2, 3) | ||
(False, False) | ||
>>> gh5643_int_optional_integer(2, 2) | ||
(False, True) | ||
""" | ||
return a == 1, b == 2 | ||
|
||
|
||
_WARNINGS = """ | ||
37:36: PEP-484 recommends 'typing.Optional[...]' for arguments that can be None. | ||
""" |