-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SIM907: Use Optional[Type] instead of Union[Type, None] (#110)
Closes #64
- Loading branch information
1 parent
1cae5b5
commit e63aa14
Showing
4 changed files
with
91 additions
and
0 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
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,59 @@ | ||
# Core Library | ||
import ast | ||
from typing import List, Tuple | ||
|
||
# First party | ||
from flake8_simplify.constants import BOOL_CONST_TYPES | ||
from flake8_simplify.utils import to_source | ||
|
||
|
||
def get_sim907(node: ast.Subscript) -> List[Tuple[int, int, str]]: | ||
""" | ||
Subscript( | ||
value=Name(id='Union', ctx=Load()), | ||
slice=Tuple( | ||
elts=[ | ||
Name(id='int', ctx=Load()), | ||
Name(id='str', ctx=Load()), | ||
Constant(value=None, kind=None), | ||
], | ||
... | ||
) | ||
) | ||
""" | ||
errors: List[Tuple[int, int, str]] = [] | ||
|
||
if not (isinstance(node.value, ast.Name) and node.value.id == "Union"): | ||
return errors | ||
|
||
if isinstance(node.slice, ast.Index) and isinstance( | ||
node.slice.value, ast.Tuple # type: ignore | ||
): | ||
# Python 3.8 | ||
tuple_var = node.slice.value # type: ignore | ||
elif isinstance(node.slice, ast.Tuple): | ||
# Python 3.9+ | ||
tuple_var = node.slice | ||
else: | ||
return errors | ||
|
||
has_none = False | ||
others = [] | ||
for elt in tuple_var.elts: # type: ignore | ||
if isinstance(elt, BOOL_CONST_TYPES) and elt.value is None: | ||
has_none = True | ||
else: | ||
others.append(elt) | ||
|
||
RULE = "SIM907 Use 'Optional[{type_}]' instead of '{original}'" | ||
if len(others) == 1 and has_none: | ||
type_ = to_source(others[0]) | ||
errors.append( | ||
( | ||
node.lineno, | ||
node.col_offset, | ||
RULE.format(type_=type_, original=to_source(node)), | ||
) | ||
) | ||
return errors |
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