From e8818d89d3a0163982db481515479618b2dc3261 Mon Sep 17 00:00:00 2001 From: wim glenn Date: Fri, 24 Nov 2023 20:44:48 -0600 Subject: [PATCH] use dict literals https://pylint.readthedocs.io/en/latest/user_guide/messages/refactor/use-dict-literal.html --- README.rst | 10 ++++---- parse.py | 52 ++++++++++++++++++++--------------------- tests/test_bugs.py | 4 ++-- tests/test_parse.py | 16 ++++++------- tests/test_parsetype.py | 8 +++---- tests/test_pattern.py | 34 +++++++++++++-------------- 6 files changed, 62 insertions(+), 62 deletions(-) diff --git a/README.rst b/README.rst index 9c3d5db..0acf31d 100644 --- a/README.rst +++ b/README.rst @@ -339,7 +339,7 @@ with the same identifier: >>> def shouty(string): ... return string.upper() ... - >>> parse('{:shouty} world', 'hello world', dict(shouty=shouty)) + >>> parse('{:shouty} world', 'hello world', {"shouty": shouty}) If the type converter has the optional ``pattern`` attribute, it is used as @@ -350,9 +350,9 @@ regular expression for better pattern matching (instead of the default one): >>> def parse_number(text): ... return int(text) >>> parse_number.pattern = r'\d+' - >>> parse('Answer: {number:Number}', 'Answer: 42', dict(Number=parse_number)) + >>> parse('Answer: {number:Number}', 'Answer: 42', {"Number": parse_number}) - >>> _ = parse('Answer: {:Number}', 'Answer: Alice', dict(Number=parse_number)) + >>> _ = parse('Answer: {:Number}', 'Answer: Alice', {"Number": parse_number}) >>> assert _ is None, "MISMATCH" You can also use the ``with_pattern(pattern)`` decorator to add this @@ -364,7 +364,7 @@ information to a type converter function: >>> @with_pattern(r'\d+') ... def parse_number(text): ... return int(text) - >>> parse('Answer: {number:Number}', 'Answer: 42', dict(Number=parse_number)) + >>> parse('Answer: {number:Number}', 'Answer: 42', {"Number": parse_number}) A more complete example of a custom type might be: @@ -390,7 +390,7 @@ in the ``with_pattern()`` decorator: >>> @with_pattern(r'((\d+))', regex_group_count=2) ... def parse_number2(text): ... return int(text) - >>> parse('Answer: {:Number2} {:Number2}', 'Answer: 42 43', dict(Number2=parse_number2)) + >>> parse('Answer: {:Number2} {:Number2}', 'Answer: 42 43', {"Number2": parse_number2}) Otherwise, this may cause parsing problems with unnamed/fixed parameters. diff --git a/parse.py b/parse.py index cebe27c..474dd68 100644 --- a/parse.py +++ b/parse.py @@ -141,31 +141,31 @@ def __eq__(self, other): return self._name == other._name and self._offset == other._offset -MONTHS_MAP = dict( - Jan=1, - January=1, - Feb=2, - February=2, - Mar=3, - March=3, - Apr=4, - April=4, - May=5, - Jun=6, - June=6, - Jul=7, - July=7, - Aug=8, - August=8, - Sep=9, - September=9, - Oct=10, - October=10, - Nov=11, - November=11, - Dec=12, - December=12, -) +MONTHS_MAP = { + "Jan": 1, + "January": 1, + "Feb": 2, + "February": 2, + "Mar": 3, + "March": 3, + "Apr": 4, + "April": 4, + "May": 5, + "Jun": 6, + "June": 6, + "Jul": 7, + "July": 7, + "Aug": 8, + "August": 8, + "Sep": 9, + "September": 9, + "Oct": 10, + "October": 10, + "Nov": 11, + "November": 11, + "Dec": 12, + "December": 12, +} DAYS_PAT = r"(Mon|Tue|Wed|Thu|Fri|Sat|Sun)" MONTHS_PAT = r"(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)" ALL_MONTHS_PAT = r"(%s)" % "|".join(MONTHS_MAP) @@ -585,7 +585,7 @@ def evaluate_result(self, m): named_fields[korig] = value # now figure the match spans - spans = dict((n, m.span(name_map[n])) for n in named_fields) + spans = {n: m.span(name_map[n]) for n in named_fields} spans.update((i, m.span(n + 1)) for i, n in enumerate(self._fixed_fields)) # and that's our result diff --git a/tests/test_bugs.py b/tests/test_bugs.py index 1dabbaf..094d3f5 100644 --- a/tests/test_bugs.py +++ b/tests/test_bugs.py @@ -56,11 +56,11 @@ def parse_number(text): return int(text) # -- CASE: Use named (OK) - type_map = dict(Name=parse_word_and_covert_to_uppercase, Number=parse_number) + type_map = {"Name": parse_word_and_covert_to_uppercase, "Number": parse_number} r = parse.parse( "Hello {name:Name} {number:Number}", "Hello Alice 42", extra_types=type_map ) - assert r.named == dict(name="ALICE", number=42) + assert r.named == {"name": "ALICE", "number": 42} # -- CASE: Use unnamed/fixed (problematic) r = parse.parse("Hello {:Name} {:Number}", "Hello Alice 42", extra_types=type_map) diff --git a/tests/test_parse.py b/tests/test_parse.py index deb4cb4..582cce3 100644 --- a/tests/test_parse.py +++ b/tests/test_parse.py @@ -126,10 +126,10 @@ def test_custom_type(): r = parse.parse( "{:shouty} {:spam}", "hello world", - dict(shouty=lambda s: s.upper(), spam=lambda s: "".join(reversed(s))), + {"shouty": lambda s: s.upper(), "spam": lambda s: "".join(reversed(s))}, ) assert r.fixed == ("HELLO", "dlrow") - r = parse.parse("{:d}", "12", dict(d=lambda s: int(s) * 2)) + r = parse.parse("{:d}", "12", {"d": lambda s: int(s) * 2}) assert r.fixed == (24,) r = parse.parse("{:d}", "12") assert r.fixed == (12,) @@ -180,25 +180,25 @@ def test_mixed(): # pull a fixed and named values out of string r = parse.parse("hello {} {name} {} {spam}", "hello world and other beings") assert r.fixed == ("world", "other") - assert r.named == dict(name="and", spam="beings") + assert r.named == {"name": "and", "spam": "beings"} def test_named_typed(): # pull a named, typed values out of string r = parse.parse("hello {number:d} {things}", "hello 12 people") - assert r.named == dict(number=12, things="people") + assert r.named == {"number": 12, "things": "people"} r = parse.parse("hello {number:w} {things}", "hello 12 people") - assert r.named == dict(number="12", things="people") + assert r.named == {"number": "12", "things": "people"} def test_named_aligned_typed(): # pull a named, typed values out of string r = parse.parse("hello {number:d} {things}", "hello 12 people") - assert r.named == dict(number=12, things="people") + assert r.named == {"number": 12, "things": "people"} r = parse.parse("hello {number:^d} {things}", "hello 12 people") - assert r.named == dict(number=12, things="people") + assert r.named == {"number": 12, "things": "people"} def test_multiline(): diff --git a/tests/test_parsetype.py b/tests/test_parsetype.py index 2a1961e..c7597f1 100644 --- a/tests/test_parsetype.py +++ b/tests/test_parsetype.py @@ -72,7 +72,7 @@ def parse_yesno(text): def test_with_pattern(): - ab_vals = dict(a=1, b=2) + ab_vals = {"a": 1, "b": 2} @parse.with_pattern(r"[ab]") def ab(text): @@ -96,7 +96,7 @@ def parse_unit(text): def parse_number(text): return int(text) - type_converters = dict(Number=parse_number, Unit=parse_unit) + type_converters = {"Number": parse_number, "Unit": parse_unit} # -- CASE: Unnamed-params (affected) parser = parse.Parser("test {:Unit}-{:Number}", type_converters) assert_fixed_match(parser, "test meter-10", ("meter", 10)) @@ -132,7 +132,7 @@ def parse_number(text): ] for bad_regex_group_count, error_class in BAD_REGEX_GROUP_COUNTS_AND_ERRORS: parse_unit.regex_group_count = bad_regex_group_count # -- OVERRIDE-HERE - type_converters = dict(Number=parse_number, Unit=parse_unit) + type_converters = {"Number": parse_number, "Unit": parse_unit} parser = parse.Parser("test {:Unit}-{:Number}", type_converters) with pytest.raises(error_class): parser.parse("test meter-10") @@ -140,7 +140,7 @@ def parse_number(text): def test_with_pattern_and_regex_group_count_is_none(): # -- CORNER-CASE: Increase code-coverage. - data_values = dict(a=1, b=2) + data_values = {"a": 1, "b": 2} @parse.with_pattern(r"[ab]") def parse_data(text): diff --git a/tests/test_pattern.py b/tests/test_pattern.py index b5f00a5..abc19d6 100644 --- a/tests/test_pattern.py +++ b/tests/test_pattern.py @@ -48,23 +48,23 @@ def _(fmt, matches): assert d.get(k) == matches[k] for t in "%obxegfdDwWsS": - _(t, dict(type=t)) - _("10" + t, dict(type=t, width="10")) - _("05d", dict(type="d", width="5", zero=True)) - _("<", dict(align="<")) - _(".<", dict(align="<", fill=".")) - _(">", dict(align=">")) - _(".>", dict(align=">", fill=".")) - _("^", dict(align="^")) - _(".^", dict(align="^", fill=".")) - _("x=d", dict(type="d", align="=", fill="x")) - _("d", dict(type="d")) - _("ti", dict(type="ti")) - _("spam", dict(type="spam")) - - _(".^010d", dict(type="d", width="10", align="^", fill=".", zero=True)) - _(".2f", dict(type="f", precision="2")) - _("10.2f", dict(type="f", width="10", precision="2")) + _(t, {"type": t}) + _("10" + t, {"type": t, "width": "10"}) + _("05d", {"type": "d", "width": "5", "zero": True}) + _("<", {"align": "<"}) + _(".<", {"align": "<", "fill": "."}) + _(">", {"align": ">"}) + _(".>", {"align": ">", "fill": "."}) + _("^", {"align": "^"}) + _(".^", {"align": "^", "fill": "."}) + _("x=d", {"type": "d", "align": "=", "fill": "x"}) + _("d", {"type": "d"}) + _("ti", {"type": "ti"}) + _("spam", {"type": "spam"}) + + _(".^010d", {"type": "d", "width": "10", "align": "^", "fill": ".", "zero": True}) + _(".2f", {"type": "f", "precision": "2"}) + _("10.2f", {"type": "f", "width": "10", "precision": "2"}) def test_dot_separated_fields():