Skip to content

Commit

Permalink
Add ability to detect os.environ in FURB123:
Browse files Browse the repository at this point in the history
There are probably other checks that could benefit from this new logic, though
lots of checks have scattered type checking logic that needs consolidating.
  • Loading branch information
dosisod committed Feb 19, 2024
1 parent 41264ac commit 12b7f84
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
24 changes: 12 additions & 12 deletions refurb/checks/readability/no_unnecessary_cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ class ErrorInfo(Error):


FUNC_NAME_MAPPING = {
"builtins.bool": (bool, ""),
"builtins.bytes": (bytes, ""),
"builtins.complex": (complex, ""),
"builtins.dict": (dict, ".copy()"),
"builtins.float": (float, ""),
"builtins.int": (int, ""),
"builtins.list": (list, ".copy()"),
"builtins.set": (set, ".copy()"),
"builtins.str": (str, ""),
"builtins.tuple": (tuple, ""),
"builtins.bool": ("", bool),
"builtins.bytes": ("", bytes),
"builtins.complex": ("", complex),
"builtins.dict": (".copy()", dict, "os._Environ"),
"builtins.float": ("", float),
"builtins.int": ("", int),
"builtins.list": (".copy()", list),
"builtins.set": (".copy()", set),
"builtins.str": ("", str),
"builtins.tuple": ("", tuple),
}


Expand All @@ -66,9 +66,9 @@ def check(node: CallExpr, errors: list[Error]) -> None:
args=[arg],
arg_kinds=[ArgKind.ARG_POS],
) if found := FUNC_NAME_MAPPING.get(fullname):
expected_type, suffix = found
suffix, *expected_types = found

if not is_same_type(get_mypy_type(arg), expected_type):
if not is_same_type(get_mypy_type(arg), *expected_types):
return

expr = stringify(arg)
Expand Down
4 changes: 4 additions & 0 deletions test/data/err_123.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def func() -> bool:
_ = set(s)
_ = set({1})

import os

_ = dict(os.environ)


# these will not

Expand Down
1 change: 1 addition & 0 deletions test/data/err_123.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ test/data/err_123.py:35:5 [FURB123]: Replace `tuple(t)` with `t`
test/data/err_123.py:40:5 [FURB123]: Replace `bool(func())` with `func()`
test/data/err_123.py:43:5 [FURB123]: Replace `set(s)` with `s.copy()`
test/data/err_123.py:44:5 [FURB123]: Replace `set({1})` with `{1}.copy()`
test/data/err_123.py:48:5 [FURB123]: Replace `dict(os.environ)` with `os.environ.copy()`

0 comments on commit 12b7f84

Please sign in to comment.