Skip to content

Commit

Permalink
Add os.environ to copy-able mapping type list
Browse files Browse the repository at this point in the history
  • Loading branch information
dosisod committed Mar 5, 2024
1 parent 85ef890 commit ee2daae
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions refurb/checks/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,7 @@ def mypy_type_to_python_type(ty: Type | SymbolNode | None) -> type | None:
"collections.abc.Mapping",
"collections.abc.MutableMapping",
"collections.defaultdict",
"os._Environ",
"typing.Mapping",
"typing.MutableMapping",
)
Expand Down
2 changes: 1 addition & 1 deletion refurb/checks/readability/no_copy_with_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def check_expr(expr: Expression, errors: list[Error]) -> None:
case CallExpr(
callee=MemberExpr(expr=lhs, name="copy"),
args=[],
) if is_same_type(get_mypy_type(lhs), dict, set):
) if is_same_type(get_mypy_type(lhs), dict, set, "os._Environ"):
msg = f"Replace `{stringify(lhs)}.copy()` with `{stringify(lhs)}`"

errors.append(ErrorInfo.from_node(expr, msg))
Expand Down
5 changes: 5 additions & 0 deletions test/data/err_173.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ def mutable_mapping_test(m: MutableMapping[str, str]):
_ = dict(**m)


import os

_ = dict(**os.environ)


# these should not

_ = {}
Expand Down
1 change: 1 addition & 0 deletions test/data/err_173.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ test/data/err_173.py:43:5 [FURB173]: Replace `dict(**x, **{})` with `x | {}`
test/data/err_173.py:48:5 [FURB173]: Replace `{**Wrapper().d, **x}` with `Wrapper().d | x`
test/data/err_173.py:54:9 [FURB173]: Replace `dict(**m)` with `{**m}`
test/data/err_173.py:57:9 [FURB173]: Replace `dict(**m)` with `{**m}`
test/data/err_173.py:62:5 [FURB173]: Replace `dict(**os.environ)` with `{**os.environ}`
4 changes: 4 additions & 0 deletions test/data/err_185.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class C:

_ = C().d.copy() | {}

import os

_ = os.environ.copy() | {}


# these should not

Expand Down
1 change: 1 addition & 0 deletions test/data/err_185.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ test/data/err_185.py:9:13 [FURB185]: Replace `y.copy()` with `y`
test/data/err_185.py:11:5 [FURB185]: Replace `x.copy()` with `x`
test/data/err_185.py:11:21 [FURB185]: Replace `x.copy()` with `x`
test/data/err_185.py:18:5 [FURB185]: Replace `C().d.copy()` with `C().d`
test/data/err_185.py:22:5 [FURB185]: Replace `os.environ.copy()` with `os.environ`

0 comments on commit ee2daae

Please sign in to comment.