Skip to content

Commit f282b93

Browse files
committed
pythongh-111696: type(str) returns the fully qualified name
* Update modules: * enum * functools * optparse * pdb * xmlrcp.server * Update tests: * test_dataclasses * test_descrtut * test_cmd_line_script
1 parent d9fd33a commit f282b93

File tree

11 files changed

+84
-39
lines changed

11 files changed

+84
-39
lines changed

Doc/whatsnew/3.13.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ Other Language Changes
125125
equivalent of the :option:`-X frozen_modules <-X>` command-line option.
126126
(Contributed by Yilei Yang in :gh:`111374`.)
127127

128+
* Formatting a type as a string now formats the type fully qualified name,
129+
instead of formating its representation (`<class ...>`).
130+
(Contributed by Victor Stinner in :gh:`111696`.)
131+
132+
128133
New Modules
129134
===========
130135

Lib/dataclasses.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ def _get_field(cls, a_name, a_type, default_kw_only):
849849
# indicator for mutability. Read the __hash__ attribute from the class,
850850
# not the instance.
851851
if f._field_type is _FIELD and f.default.__class__.__hash__ is None:
852-
raise ValueError(f'mutable default {type(f.default)} for field '
852+
raise ValueError(f'mutable default {type(f.default)!r} for field '
853853
f'{f.name} is not allowed: use default_factory')
854854

855855
return f

Lib/enum.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ def __call__(cls, value, names=None, *values, module=None, qualname=None, type=N
729729
if names is None and type is None:
730730
# no body? no data-type? possibly wrong usage
731731
raise TypeError(
732-
f"{cls} has no members; specify `names=()` if you meant to create a new, empty, enum"
732+
f"{cls!r} has no members; specify `names=()` if you meant to create a new, empty, enum"
733733
)
734734
return cls._create_(
735735
class_name=value,

Lib/functools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ def _find_impl(cls, registry):
793793
and match not in cls.__mro__
794794
and not issubclass(match, t)):
795795
raise RuntimeError("Ambiguous dispatch: {} or {}".format(
796-
match, t))
796+
repr(match), repr(t)))
797797
break
798798
if t in registry:
799799
match = t

Lib/optparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ def _check_choice(self):
667667
elif not isinstance(self.choices, (tuple, list)):
668668
raise OptionError(
669669
"choices must be a list of strings ('%s' supplied)"
670-
% str(type(self.choices)).split("'")[1], self)
670+
% repr(type(self.choices)).split("'")[1], self)
671671
elif self.choices is not None:
672672
raise OptionError(
673673
"must not supply choices for type %r" % self.type, self)

Lib/pdb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1729,7 +1729,7 @@ def do_whatis(self, arg):
17291729
self.message('Class %s.%s' % (value.__module__, value.__qualname__))
17301730
return
17311731
# None of the above...
1732-
self.message(type(value))
1732+
self.message(repr(type(value)))
17331733

17341734
complete_whatis = _complete_expression
17351735

Lib/test/test_cmd_line_script.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def _check_import_error(self, script_exec_args, expected_msg,
153153
self.assertIn(expected_msg.encode('utf-8'), err)
154154

155155
def test_dash_c_loader(self):
156-
rc, out, err = assert_python_ok("-c", "print(__loader__)")
156+
rc, out, err = assert_python_ok("-c", "print(repr(__loader__))")
157157
expected = repr(importlib.machinery.BuiltinImporter).encode("utf-8")
158158
self.assertIn(expected, out)
159159

@@ -163,7 +163,7 @@ def test_stdin_loader(self):
163163
# stdin is an interactive tty.
164164
p = spawn_python()
165165
try:
166-
p.stdin.write(b"print(__loader__)\n")
166+
p.stdin.write(b"print(repr(__loader__))\n")
167167
p.stdin.flush()
168168
finally:
169169
out = kill_python(p)

Lib/test/test_dataclasses/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ def test_disallowed_mutable_defaults(self):
738738
with self.subTest(typ=typ):
739739
# Can't use a zero-length value.
740740
with self.assertRaisesRegex(ValueError,
741-
f'mutable default {typ} for field '
741+
f'mutable default {typ!r} for field '
742742
'x is not allowed'):
743743
@dataclass
744744
class Point:
@@ -747,7 +747,7 @@ class Point:
747747

748748
# Nor a non-zero-length value
749749
with self.assertRaisesRegex(ValueError,
750-
f'mutable default {typ} for field '
750+
f'mutable default {typ!r} for field '
751751
'y is not allowed'):
752752
@dataclass
753753
class Point:

Lib/test/test_descrtut.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,16 @@ def merge(self, other):
3838
3939
Here's the new type at work:
4040
41-
>>> print(defaultdict) # show our type
41+
>>> print(repr(defaultdict)) # show our type
4242
<class 'test.test_descrtut.defaultdict'>
43-
>>> print(type(defaultdict)) # its metatype
43+
>>> print(repr(type(defaultdict))) # its metatype
4444
<class 'type'>
4545
>>> a = defaultdict(default=0.0) # create an instance
4646
>>> print(a) # show the instance
4747
{}
48-
>>> print(type(a)) # show its type
48+
>>> print(repr(type(a))) # show its type
4949
<class 'test.test_descrtut.defaultdict'>
50-
>>> print(a.__class__) # show its class
50+
>>> print(repr(a.__class__)) # show its class
5151
<class 'test.test_descrtut.defaultdict'>
5252
>>> print(type(a) is a.__class__) # its type is its class
5353
True
@@ -261,7 +261,7 @@ def merge(self, other):
261261
>>> class C:
262262
... @classmethod
263263
... def foo(cls, y):
264-
... print("classmethod", cls, y)
264+
... print("classmethod", repr(cls), y)
265265
266266
>>> C.foo(1)
267267
classmethod <class 'test.test_descrtut.C'> 1

Lib/xmlrpc/server.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def _marshaled_dispatch(self, data, dispatch_method = None, path = None):
270270
encoding=self.encoding)
271271
except BaseException as exc:
272272
response = dumps(
273-
Fault(1, "%s:%s" % (type(exc), exc)),
273+
Fault(1, "%s:%s" % (repr(type(exc)), exc)),
274274
encoding=self.encoding, allow_none=self.allow_none,
275275
)
276276

@@ -365,7 +365,7 @@ def system_multicall(self, call_list):
365365
except BaseException as exc:
366366
results.append(
367367
{'faultCode' : 1,
368-
'faultString' : "%s:%s" % (type(exc), exc)}
368+
'faultString' : "%s:%s" % (repr(type(exc)), exc)}
369369
)
370370
return results
371371

@@ -628,7 +628,7 @@ def _marshaled_dispatch(self, data, dispatch_method = None, path = None):
628628
# (each dispatcher should have handled their own
629629
# exceptions)
630630
response = dumps(
631-
Fault(1, "%s:%s" % (type(exc), exc)),
631+
Fault(1, "%s:%s" % (repr(type(exc)), exc)),
632632
encoding=self.encoding, allow_none=self.allow_none)
633633
response = response.encode(self.encoding, 'xmlcharrefreplace')
634634
return response

0 commit comments

Comments
 (0)