Skip to content

Commit 82dd956

Browse files
committed
Use type.__fullyqualname__ attribute
1 parent d5e8a37 commit 82dd956

21 files changed

+35
-46
lines changed

Lib/_collections_abc.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -528,9 +528,7 @@ def _type_repr(obj):
528528
(Keep this roughly in sync with the typing version.)
529529
"""
530530
if isinstance(obj, type):
531-
if obj.__module__ == 'builtins':
532-
return obj.__qualname__
533-
return f'{obj.__module__}.{obj.__qualname__}'
531+
return obj.__fullyqualname__
534532
if obj is Ellipsis:
535533
return '...'
536534
if isinstance(obj, FunctionType):

Lib/_py_abc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def register(cls, subclass):
7171

7272
def _dump_registry(cls, file=None):
7373
"""Debug helper to print the ABC registry."""
74-
print(f"Class: {cls.__module__}.{cls.__qualname__}", file=file)
74+
print(f"Class: {cls.__fullyqualname__}", file=file)
7575
print(f"Inv. counter: {get_cache_token()}", file=file)
7676
for name in cls.__dict__:
7777
if name.startswith("_abc_"):

Lib/abc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def __subclasscheck__(cls, subclass):
124124

125125
def _dump_registry(cls, file=None):
126126
"""Debug helper to print the ABC registry."""
127-
print(f"Class: {cls.__module__}.{cls.__qualname__}", file=file)
127+
print(f"Class: {cls.__fullyqualname__}", file=file)
128128
print(f"Inv. counter: {get_cache_token()}", file=file)
129129
(_abc_registry, _abc_cache, _abc_negative_cache,
130130
_abc_negative_cache_version) = _get_dump(cls)

Lib/codecs.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ def __new__(cls, encode, decode, streamreader=None, streamwriter=None,
107107
return self
108108

109109
def __repr__(self):
110-
return "<%s.%s object for encoding %s at %#x>" % \
111-
(self.__class__.__module__, self.__class__.__qualname__,
110+
return "<%s object for encoding %s at %#x>" % \
111+
(self.__class__.__fullyqualname__,
112112
self.name, id(self))
113113

114114
def __getnewargs__(self):

Lib/contextlib.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ def enter_context(self, cm):
525525
_enter = cls.__enter__
526526
_exit = cls.__exit__
527527
except AttributeError:
528-
raise TypeError(f"'{cls.__module__}.{cls.__qualname__}' object does "
528+
raise TypeError(f"'{cls.__fullyqualname__}' object does "
529529
f"not support the context manager protocol") from None
530530
result = _enter(cm)
531531
self._push_cm_exit(cm, _exit)
@@ -662,7 +662,7 @@ async def enter_async_context(self, cm):
662662
_enter = cls.__aenter__
663663
_exit = cls.__aexit__
664664
except AttributeError:
665-
raise TypeError(f"'{cls.__module__}.{cls.__qualname__}' object does "
665+
raise TypeError(f"'{cls.__fullyqualname__}' object does "
666666
f"not support the asynchronous context manager protocol"
667667
) from None
668668
result = await _enter(cm)

Lib/doctest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1401,7 +1401,7 @@ def __run(self, test, compileflags, out):
14011401
# They start with `SyntaxError:` (or any other class name)
14021402
exception_line_prefixes = (
14031403
f"{exception[0].__qualname__}:",
1404-
f"{exception[0].__module__}.{exception[0].__qualname__}:",
1404+
f"{exception[0].__fullyqualname__}:",
14051405
)
14061406
exc_msg_index = next(
14071407
index

Lib/inspect.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1501,9 +1501,9 @@ def repl(match):
15011501
if isinstance(annotation, types.GenericAlias):
15021502
return str(annotation)
15031503
if isinstance(annotation, type):
1504-
if annotation.__module__ in ('builtins', base_module):
1504+
if annotation.__module__ == base_module:
15051505
return annotation.__qualname__
1506-
return annotation.__module__+'.'+annotation.__qualname__
1506+
return annotation.__fullyqualname__
15071507
return repr(annotation)
15081508

15091509
def formatannotationrelativeto(object):

Lib/multiprocessing/pool.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ def __del__(self, _warn=warnings.warn, RUN=RUN):
272272

273273
def __repr__(self):
274274
cls = self.__class__
275-
return (f'<{cls.__module__}.{cls.__qualname__} '
275+
return (f'<{cls.__fullyqualname__} '
276276
f'state={self._state} '
277277
f'pool_size={len(self._pool)}>')
278278

Lib/pdb.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1726,7 +1726,7 @@ def do_whatis(self, arg):
17261726
return
17271727
# Is it a class?
17281728
if value.__class__ is type:
1729-
self.message('Class %s.%s' % (value.__module__, value.__qualname__))
1729+
self.message(f'Class {value.__fullyqualname__}')
17301730
return
17311731
# None of the above...
17321732
self.message(type(value))

Lib/test/support/asyncore.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ def __init__(self, sock=None, map=None):
256256
self.socket = None
257257

258258
def __repr__(self):
259-
status = [self.__class__.__module__+"."+self.__class__.__qualname__]
259+
status = [self.__class__.__fullyqualname__]
260260
if self.accepting and self.addr:
261261
status.append('listening')
262262
elif self.connected:

Lib/test/test_configparser.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,7 @@ def get_error(self, cf, exc, section, option):
587587
except exc as e:
588588
return e
589589
else:
590-
self.fail("expected exception type %s.%s"
591-
% (exc.__module__, exc.__qualname__))
590+
self.fail(f"expected exception type {exc.__fullyqualname__}")
592591

593592
def test_boolean(self):
594593
cf = self.fromstring(

Lib/test/test_traceback.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,10 @@ def __str__(self):
209209
err = traceback.format_exception_only(X, X())
210210
self.assertEqual(len(err), 1)
211211
str_value = '<exception str() failed>'
212-
if X.__module__ in ('__main__', 'builtins'):
212+
if X.__module__ == '__main__':
213213
str_name = X.__qualname__
214214
else:
215-
str_name = '.'.join([X.__module__, X.__qualname__])
215+
str_name = X.__fullyqualname__
216216
self.assertEqual(err[0], "%s: %s\n" % (str_name, str_value))
217217

218218
def test_format_exception_group_without_show_group(self):
@@ -1875,7 +1875,7 @@ def __str__(self):
18751875

18761876
err = self.get_report(A.B.X())
18771877
str_value = 'I am X'
1878-
str_name = '.'.join([A.B.X.__module__, A.B.X.__qualname__])
1878+
str_name = A.B.X.__fullyqualname__
18791879
exp = "%s: %s\n" % (str_name, str_value)
18801880
self.assertEqual(exp, MODULE_PREFIX + err)
18811881

@@ -1889,10 +1889,10 @@ def __str__(self):
18891889
with self.subTest(modulename=modulename):
18901890
err = self.get_report(X())
18911891
str_value = 'I am X'
1892-
if modulename in ['builtins', '__main__']:
1892+
if modulename == '__main__':
18931893
str_name = X.__qualname__
18941894
else:
1895-
str_name = '.'.join([X.__module__, X.__qualname__])
1895+
str_name = X.__fullyqualname__
18961896
exp = "%s: %s\n" % (str_name, str_value)
18971897
self.assertEqual(exp, err)
18981898

@@ -1928,7 +1928,7 @@ def __str__(self):
19281928
1/0
19291929
err = self.get_report(X())
19301930
str_value = '<exception str() failed>'
1931-
str_name = '.'.join([X.__module__, X.__qualname__])
1931+
str_name = X.__fullyqualname__
19321932
self.assertEqual(MODULE_PREFIX + err, f"{str_name}: {str_value}\n")
19331933

19341934

Lib/test/test_zipimport_support.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def _run_object_doctest(obj, module):
3939
# Use the object's fully qualified name if it has one
4040
# Otherwise, use the module's name
4141
try:
42-
name = "%s.%s" % (obj.__module__, obj.__qualname__)
42+
name = obj.__fullyqualname__
4343
except AttributeError:
4444
name = module.__name__
4545
for example in finder.find(obj, name, module):

Lib/threading.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ def __init__(self, value=1):
457457

458458
def __repr__(self):
459459
cls = self.__class__
460-
return (f"<{cls.__module__}.{cls.__qualname__} at {id(self):#x}:"
460+
return (f"<{cls.__fullyqualname__} at {id(self):#x}:"
461461
f" value={self._value}>")
462462

463463
def acquire(self, blocking=True, timeout=None):
@@ -547,7 +547,7 @@ def __init__(self, value=1):
547547

548548
def __repr__(self):
549549
cls = self.__class__
550-
return (f"<{cls.__module__}.{cls.__qualname__} at {id(self):#x}:"
550+
return (f"<{cls.__fullyqualname__} at {id(self):#x}:"
551551
f" value={self._value}/{self._initial_value}>")
552552

553553
def release(self, n=1):
@@ -587,7 +587,7 @@ def __init__(self):
587587
def __repr__(self):
588588
cls = self.__class__
589589
status = 'set' if self._flag else 'unset'
590-
return f"<{cls.__module__}.{cls.__qualname__} at {id(self):#x}: {status}>"
590+
return f"<{cls.__fullyqualname__} at {id(self):#x}: {status}>"
591591

592592
def _at_fork_reinit(self):
593593
# Private method called by Thread._after_fork()
@@ -690,8 +690,8 @@ def __init__(self, parties, action=None, timeout=None):
690690
def __repr__(self):
691691
cls = self.__class__
692692
if self.broken:
693-
return f"<{cls.__module__}.{cls.__qualname__} at {id(self):#x}: broken>"
694-
return (f"<{cls.__module__}.{cls.__qualname__} at {id(self):#x}:"
693+
return f"<{cls.__fullyqualname__} at {id(self):#x}: broken>"
694+
return (f"<{cls.__fullyqualname__} at {id(self):#x}:"
695695
f" waiters={self.n_waiting}/{self.parties}>")
696696

697697
def wait(self, timeout=None):

Lib/tkinter/__init__.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1802,8 +1802,7 @@ def __str__(self):
18021802
return self._w
18031803

18041804
def __repr__(self):
1805-
return '<%s.%s object %s>' % (
1806-
self.__class__.__module__, self.__class__.__qualname__, self._w)
1805+
return f'<{self.__class__.__fullyqualname__} object {self._w}>'
18071806

18081807
# Pack methods that apply to the master
18091808
_noarg_ = ['_noarg_']

Lib/tkinter/font.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ def __str__(self):
101101
return self.name
102102

103103
def __repr__(self):
104-
return f"<{self.__class__.__module__}.{self.__class__.__qualname__}" \
105-
f" object {self.name!r}>"
104+
return (f"<{self.__class__.__fullyqualname__}"
105+
f" object {self.name!r}>")
106106

107107
def __eq__(self, other):
108108
if not isinstance(other, Font):

Lib/typing.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,7 @@ def _type_repr(obj):
236236
# `_collections_abc._type_repr`, which does the same thing
237237
# and must be consistent with this one.
238238
if isinstance(obj, type):
239-
if obj.__module__ == 'builtins':
240-
return obj.__qualname__
241-
return f'{obj.__module__}.{obj.__qualname__}'
239+
return obj.__fullyqualname__
242240
if obj is ...:
243241
return '...'
244242
if isinstance(obj, types.FunctionType):
@@ -1402,10 +1400,7 @@ def __init__(self, origin, nparams, *, inst=True, name=None):
14021400
name = origin.__name__
14031401
super().__init__(origin, inst=inst, name=name)
14041402
self._nparams = nparams
1405-
if origin.__module__ == 'builtins':
1406-
self.__doc__ = f'A generic version of {origin.__qualname__}.'
1407-
else:
1408-
self.__doc__ = f'A generic version of {origin.__module__}.{origin.__qualname__}.'
1403+
self.__doc__ = f'A generic version of {origin.__fullyqualname__}.'
14091404

14101405
@_tp_cache
14111406
def __getitem__(self, params):

Lib/unittest/async_case.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ async def enterAsyncContext(self, cm):
7474
enter = cls.__aenter__
7575
exit = cls.__aexit__
7676
except AttributeError:
77-
raise TypeError(f"'{cls.__module__}.{cls.__qualname__}' object does "
77+
raise TypeError(f"'{cls.__fullyqualname__}' object does "
7878
f"not support the asynchronous context manager protocol"
7979
) from None
8080
result = await enter(cm)

Lib/unittest/case.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def _enter_context(cm, addcleanup):
111111
enter = cls.__enter__
112112
exit = cls.__exit__
113113
except AttributeError:
114-
raise TypeError(f"'{cls.__module__}.{cls.__qualname__}' object does "
114+
raise TypeError(f"'{cls.__fullyqualname__}' object does "
115115
f"not support the context manager protocol") from None
116116
result = enter(cm)
117117
addcleanup(exit, cm, None, None, None)

Lib/unittest/loader.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,7 @@ def shouldIncludeMethod(attrname):
216216
testFunc = getattr(testCaseClass, attrname)
217217
if not callable(testFunc):
218218
return False
219-
fullName = f'%s.%s.%s' % (
220-
testCaseClass.__module__, testCaseClass.__qualname__, attrname
221-
)
219+
fullName = f'{testCaseClass.__fullyqualname__}.{attrname}'
222220
return self.testNamePatterns is None or \
223221
any(fnmatchcase(fullName, pattern) for pattern in self.testNamePatterns)
224222
testFnNames = list(filter(shouldIncludeMethod, dir(testCaseClass)))

Lib/unittest/util.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def safe_repr(obj, short=False):
5252
return result[:_MAX_LENGTH] + ' [truncated]...'
5353

5454
def strclass(cls):
55-
return "%s.%s" % (cls.__module__, cls.__qualname__)
55+
return cls.__fullyqualname__
5656

5757
def sorted_list_difference(expected, actual):
5858
"""Finds elements in only one or the other of two, sorted input lists.

0 commit comments

Comments
 (0)