Skip to content

Commit bec8cba

Browse files
authored
Fix crash when missing format character (#20524)
Fixes #20523 ``` >>> import re >>> m = re.fullmatch(r"%(?P<type>.)?", "%") >>> m.groupdict() {'type': None} >>> m.groupdict().get("type", "") >>> m.groupdict().get("type") or "" '' ```
1 parent c71fdb0 commit bec8cba

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

mypy/checkstrformat.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,10 @@ def __init__(
145145
self.key = m_dict.get("key")
146146

147147
# Replace unmatched optional groups with empty matches (for convenience).
148-
self.conv_type = m_dict.get("type", "")
149-
self.flags = m_dict.get("flags", "")
150-
self.width = m_dict.get("width", "")
151-
self.precision = m_dict.get("precision", "")
148+
self.conv_type = m_dict.get("type") or ""
149+
self.flags = m_dict.get("flags") or ""
150+
self.width = m_dict.get("width") or ""
151+
self.precision = m_dict.get("precision") or ""
152152

153153
# Used only for str.format() calls (it may be custom for types with __format__()).
154154
self.format_spec = m_dict.get("format_spec")

test-data/unit/check-formatting.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ a = None # type: Any
6060
[case testStringInterpolationInvalidPlaceholder]
6161
'%W' % 1 # E: Unsupported format character "W"
6262
'%b' % 1 # E: Format character "b" is only supported on bytes patterns
63+
'%(a)' % {'a': 1} # E: Unsupported format character ""
6364

6465
[case testStringInterpolationWidth]
6566
'%2f' % 3.14

0 commit comments

Comments
 (0)