diff --git a/cssutils/__init__.py b/cssutils/__init__.py index 277704c1..38d22353 100644 --- a/cssutils/__init__.py +++ b/cssutils/__init__.py @@ -133,7 +133,7 @@ def createCSSStyleSheet(self, title, media): "https://web.archive.org/web/20200701035537/" "https://bitbucket.org/cthedot/cssutils/issues/69#comment-30669799" ) - warnings.warn(warning, DeprecationWarning) + warnings.warn(warning, DeprecationWarning, stacklevel=2) return css.CSSStyleSheet(title=title, media=media) def createDocument(self, *args, **kwargs): diff --git a/cssutils/css/cssrule.py b/cssutils/css/cssrule.py index 2fd30adc..ff07f53d 100644 --- a/cssutils/css/cssrule.py +++ b/cssutils/css/cssrule.py @@ -174,7 +174,7 @@ def _setCssRules(self, cssRules): "Set new cssRules and update contained rules refs." cssRules.append = self.insertRule cssRules.extend = self.insertRule - cssRules.__delitem__ == self.deleteRule + cssRules.__delitem__ = self.deleteRule for rule in cssRules: rule._parentRule = self @@ -225,12 +225,12 @@ def deleteRule(self, index): self._cssRules[index]._parentRule = None del self._cssRules[index] - except IndexError: + except IndexError as err: raise xml.dom.IndexSizeErr( '%s: %s is not a valid index ' 'in the rulelist of length %i' % (self.__class__.__name__, index, self._cssRules.length) - ) + ) from err def _prepareInsertRule(self, rule, index=None): "return checked `index` and optional parsed `rule`" diff --git a/cssutils/css/cssstylesheet.py b/cssutils/css/cssstylesheet.py index b4bc4c6b..e8c88a5b 100644 --- a/cssutils/css/cssstylesheet.py +++ b/cssutils/css/cssstylesheet.py @@ -526,11 +526,11 @@ def deleteRule(self, index): try: rule = self._cssRules[index] - except IndexError: + except IndexError as err: raise xml.dom.IndexSizeErr( 'CSSStyleSheet: %s is not a valid index in the rulelist of ' 'length %i' % (index, self._cssRules.length) - ) + ) from err else: if rule.type == rule.NAMESPACE_RULE: # check all namespacerules if used diff --git a/cssutils/css/cssvalue.py b/cssutils/css/cssvalue.py index c88e7849..a386bd0d 100644 --- a/cssutils/css/cssvalue.py +++ b/cssutils/css/cssvalue.py @@ -560,9 +560,7 @@ def __init__(self, cssText=None, parent=None, readonly=False): super().__init__(cssText=cssText, parent=parent, readonly=readonly) def __str__(self): - return ( - f"" - ) + return f"" _unitnames = [ 'CSS_UNKNOWN', @@ -696,10 +694,10 @@ def _getNumDim(self, value=None): val = float(val) if val == int(val): val = int(val) - except ValueError: + except ValueError as err: raise xml.dom.InvalidAccessErr( 'CSSPrimitiveValue: No float value %r' % self._value[0] - ) + ) from err return val, dim @@ -731,14 +729,14 @@ def getFloatValue(self, unitType=None): # convert if needed try: val = self._converter[self.primitiveType, unitType](val) - except KeyError: + except KeyError as err: raise xml.dom.InvalidAccessErr( 'CSSPrimitiveValue: Cannot coerce primitiveType %r to %r' % ( self.primitiveTypeString, self._getCSSPrimitiveTypeString(unitType), ) - ) + ) from err if val == int(val): val = int(val) @@ -775,24 +773,24 @@ def setFloatValue(self, unitType, floatValue): ) try: val = float(floatValue) - except ValueError: + except ValueError as err: raise xml.dom.InvalidAccessErr( 'CSSPrimitiveValue: floatValue %r is not a float' % floatValue - ) + ) from err oldval, dim = self._getNumDim() if self.primitiveType != unitType: # convert if possible try: val = self._converter[unitType, self.primitiveType](val) - except KeyError: + except KeyError as err: raise xml.dom.InvalidAccessErr( 'CSSPrimitiveValue: Cannot coerce primitiveType %r to %r' % ( self.primitiveTypeString, self._getCSSPrimitiveTypeString(unitType), ) - ) + ) from err if val == int(val): val = int(val) diff --git a/cssutils/css2productions.py b/cssutils/css2productions.py index 3eadb95c..9a1d59b7 100644 --- a/cssutils/css2productions.py +++ b/cssutils/css2productions.py @@ -127,5 +127,5 @@ class CSSProductions: pass -for i, t in enumerate(PRODUCTIONS): +for t in PRODUCTIONS: setattr(CSSProductions, t[0].replace('-', '_'), t[0]) diff --git a/cssutils/cssproductions.py b/cssutils/cssproductions.py index 02541266..0cbba821 100644 --- a/cssutils/cssproductions.py +++ b/cssutils/cssproductions.py @@ -111,7 +111,7 @@ class CSSProductions: VARIABLES_SYM = 'VARIABLES_SYM' -for i, t in enumerate(PRODUCTIONS): +for t in PRODUCTIONS: setattr(CSSProductions, t[0].replace('-', '_'), t[0]) diff --git a/cssutils/prodparser.py b/cssutils/prodparser.py index 4be27127..2d8f583f 100644 --- a/cssutils/prodparser.py +++ b/cssutils/prodparser.py @@ -329,7 +329,7 @@ def toStore(store, item): else: self.toSeq = lambda t, tokens: (t[0], t[1]) - if hasattr(toStore, '__call__'): + if callable(toStore): self.toStore = toStore elif toStore: self.toStore = makeToStore(toStore) diff --git a/cssutils/tests/test_cssrule.py b/cssutils/tests/test_cssrule.py index be657de3..a7eab72b 100644 --- a/cssutils/tests/test_cssrule.py +++ b/cssutils/tests/test_cssrule.py @@ -98,12 +98,12 @@ def test(s): test(s) # sheet.add CSS s = cssutils.css.CSSStyleSheet() - for css, type_ in rules: + for css, _ in rules: s.add(css) test(s) # sheet.insertRule CSS s = cssutils.css.CSSStyleSheet() - for css, type_ in rules: + for css, _ in rules: s.insertRule(css) test(s) @@ -120,14 +120,14 @@ def test(s): ] # sheet.add CSSRule s = cssutils.css.CSSStyleSheet() - for i, (css, type_) in enumerate(rules): + for i, (css, _) in enumerate(rules): rule = types[i]() rule.cssText = css s.add(rule) test(s) # sheet.insertRule CSSRule s = cssutils.css.CSSStyleSheet() - for i, (css, type_) in enumerate(rules): + for i, (css, _) in enumerate(rules): rule = types[i]() rule.cssText = css s.insertRule(rule) @@ -165,12 +165,12 @@ def getMediaSheet(): # sheet.add CSS s, mr = getMediaSheet() - for css, type_ in rules: + for css, _ in rules: mr.add(css) test(s) # sheet.insertRule CSS s, mr = getMediaSheet() - for css, type_ in rules: + for css, _ in rules: mr.insertRule(css) test(s) @@ -181,14 +181,14 @@ def getMediaSheet(): ] # sheet.add CSSRule s, mr = getMediaSheet() - for i, (css, type_) in enumerate(rules): + for i, (css, _) in enumerate(rules): rule = types[i]() rule.cssText = css mr.add(rule) test(s) # sheet.insertRule CSSRule s, mr = getMediaSheet() - for i, (css, type_) in enumerate(rules): + for i, (css, _) in enumerate(rules): rule = types[i]() rule.cssText = css mr.insertRule(rule) diff --git a/cssutils/tests/test_cssstylesheet.py b/cssutils/tests/test_cssstylesheet.py index 53285f05..0f33992f 100644 --- a/cssutils/tests/test_cssstylesheet.py +++ b/cssutils/tests/test_cssstylesheet.py @@ -84,7 +84,7 @@ def test_refs(self): ).cssRules # new object assert rules != s.cssRules - for i, r in enumerate(s.cssRules): + for r in s.cssRules: assert r.parentStyleSheet == s # namespaces diff --git a/cssutils/tests/test_cssvalue.py b/cssutils/tests/test_cssvalue.py index 8a3df4a6..5dbb0c5a 100644 --- a/cssutils/tests/test_cssvalue.py +++ b/cssutils/tests/test_cssvalue.py @@ -68,7 +68,7 @@ def test_cssText(self): assert v.CSS_VALUE_LIST == v.cssValueType assert 'normal 1px a, b, "c" end' == v.cssText - for i, x in enumerate(v): + for x in v: assert x.CSS_PRIMITIVE_VALUE == x.cssValueType if x == 0: assert x.CSS_IDENT == x.primitiveType diff --git a/cssutils/tests/test_prodparser.py b/cssutils/tests/test_prodparser.py index cc9d2580..d3dff8d1 100644 --- a/cssutils/tests/test_prodparser.py +++ b/cssutils/tests/test_prodparser.py @@ -245,7 +245,7 @@ def test_nextProd(self): } for seqitems, results in list(tests.items()): for result in results: - seq = Sequence(minmax=lambda: (1, 2), *seqitems) + seq = Sequence(*seqitems, minmax=lambda: (1, 2)) for t, p in result: if isinstance(p, str): with pytest.raises(ParseError, match=re.escape(p)): diff --git a/cssutils/tests/test_profiles.py b/cssutils/tests/test_profiles.py index 8c42c67d..688ad9c0 100644 --- a/cssutils/tests/test_profiles.py +++ b/cssutils/tests/test_profiles.py @@ -688,7 +688,7 @@ def test_validate(self): ), } # TODO!!! - for (name, values), (valid, matching, profile) in list(tests.items()): + for (name, values), (valid, _matching, _profile) in list(tests.items()): for value in values: assert valid == cssutils.profile.validate(name, value) diff --git a/cssutils/tests/test_value.py b/cssutils/tests/test_value.py index aa23de90..6cd0c999 100644 --- a/cssutils/tests/test_value.py +++ b/cssutils/tests/test_value.py @@ -709,7 +709,7 @@ def test_cssText(self): ), 'var(C, #f00 )': ('var(C, #f00)', 'C', '#fff'), } - for var, (cssText, name, fallback) in list(tests.items()): + for var, (cssText, name, _fallback) in list(tests.items()): v = cssutils.css.CSSVariable(var) assert cssText == v.cssText assert 'VARIABLE' == v.type diff --git a/tools/try.py b/tools/try.py index d6f3167a..27c47c6f 100644 --- a/tools/try.py +++ b/tools/try.py @@ -130,7 +130,7 @@ def maketokens(valuelist): font-family : a b; } ''' - cssutils.parseString(s).cssText + cssutils.parseString(s).cssText # noqa: B018 sys.exit(1) import cssutils