The traceback module omits the type module if the module is builtins or __main__. So what you are asking.
Currently, repr(type) only omits the module if the module is builtins. Should we modify repr(type) to also omit the module if the module is __main__?
Example of script:
class MyTime:
pass
print(repr(MyTime))
Output:
<class '__main__.MyTime'>
If we omit the __main__ module:
<class 'MyTime'>
It would break code which rely on the current repr(type) behavior, no?
But I would be fine to have a different format between repr(type) and type.__fullyqualname__. For me, type.__fullyqualname__ is more for end users, and repr(type) is more for debug.
Yeah, that’s one reason why I think a format argument is better than __fullyqualname__. Then you could have just T, or M.T and M:T (with both module name and separator elided for __main__/__builtins__).
I’m still -1 on adding type.__fullyqualname__ (I don’t think it’s useful to encourage potentially having builtin types with the same name redefined in other modules with the same name), but if you want still put this in, please use a different format code, e.g. “%Q” or one of the existing modifiers such as “l” to have “%lT” return the fully qualified name.
The “#” modifier you are suggesting clashes with the meaning for the one we already have for string formatting: string — Common string operations — Python 3.12.0 documentation In your case you are returning a longer version of the type name, not an alternative formatting.
You should also be careful with using type.__fullyqualname__ in your PR. Changing the formatting of strings which already do include the module name is fine, but changing the return value of methods / functions (such as your change in Lib/typing.py) can break things.
I don’t think that M.T or M:T formats fit well with current f-string format, especially with the “Format Specification” part (the part after : delimiter).
The colon (:) is used to separated the expression from the format specification. Example: : in f"{3.14:g}".
Usually, there is a single letter for the format type. Like g in f"{3.14:g}".
I also sounds surprising to me to ask for M.T or M:T format and omit the module if type.__module__ is equal to "builtins". Or do you want for format the str type as "builtins.str"?
If you want a colon separator, you can use the f-string f"{type.__module__}:{type.__qualname__}", no? You may just skip the module is it’s builtins.
The stdlib has already multiple functions which create a fully qualified name of a type using f-string f"{type.__module__}.{type.__qualname__}", and omit the module if it’s builtins.