From 7b73c9da485c87d6cd9804ec5e605169f6a3b39b Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen Date: Fri, 13 Nov 2020 19:59:09 +0100 Subject: [PATCH] Exclude types.MappingProxyType() from B008 (#144) * Exclude types.MappingProxyType() from B008 * Update README.rst --- README.rst | 1 + bugbear.py | 7 ++++++- tests/b006_b008.py | 12 ++++++++++++ tests/test_bugbear.py | 14 +++++++------- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/README.rst b/README.rst index f6faefe..291d489 100644 --- a/README.rst +++ b/README.rst @@ -253,6 +253,7 @@ Next version ~~~~~~~~~~~~ * Introduce B016 to check for raising a literal. (#141) +* Exclude types.MappingProxyType() from B008. (#144) 20.1.4 ~~~~~~ diff --git a/bugbear.py b/bugbear.py index aabf9e5..fc5c537 100644 --- a/bugbear.py +++ b/bugbear.py @@ -632,7 +632,12 @@ def visit(self, node): "use that variable as a default value." ) ) -B008.immutable_calls = {"tuple", "frozenset"} +B008.immutable_calls = { + "tuple", + "frozenset", + "types.MappingProxyType", + "MappingProxyType", +} B009 = Error( message=( "B009 Do not call getattr with a constant attribute value, " diff --git a/tests/b006_b008.py b/tests/b006_b008.py index 5a147e4..99a18dc 100644 --- a/tests/b006_b008.py +++ b/tests/b006_b008.py @@ -1,6 +1,8 @@ import collections import logging import time +import types +from types import MappingProxyType def this_is_okay(value=(1, 2, 3)): @@ -11,6 +13,16 @@ def and_this_also(value=tuple()): pass +def frozenset_also_okay(value=frozenset()): + pass + + +def mappingproxytype_okay( + value=MappingProxyType({}), value2=types.MappingProxyType({}) +): + pass + + def this_is_wrong(value=[1, 2, 3]): ... diff --git a/tests/test_bugbear.py b/tests/test_bugbear.py index be4fb0a..d2907dc 100644 --- a/tests/test_bugbear.py +++ b/tests/test_bugbear.py @@ -97,13 +97,13 @@ def test_b006_b008(self): self.assertEqual( errors, self.errors( - B006(14, 24), - B006(18, 29), - B006(22, 19), - B006(26, 19), - B006(30, 31), - B008(39, 38), - B006(55, 32), + B006(26, 24), + B006(30, 29), + B006(34, 19), + B006(38, 19), + B006(42, 31), + B008(51, 38), + B006(67, 32), ), )