← 返回首页
gh-100129: Make the names of all classes in the types module resolvable by serhiy-storchaka · Pull Request #100130 · python/cpython · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus

gh-100129: Make the names of all classes in the types module resolvable#100130

Open
serhiy-storchaka wants to merge 11 commits into
python:mainfrom
serhiy-storchaka:types-qualnames
Open

gh-100129: Make the names of all classes in the types module resolvable#100130
serhiy-storchaka wants to merge 11 commits into
python:mainfrom
serhiy-storchaka:types-qualnames

Conversation

serhiy-storchaka commented Dec 9, 2022
edited by bedevere-bot
Loading

Copy link
Copy Markdown
Member

python deleted a comment from netlify Bot Dec 10, 2022

JelleZijlstra left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

I like how this change makes the builtin types more consistent and easy to resolve. However, I have two concerns:

  • When the new type names appear in error messages, they make the errors harder to understand for new users. This is especially important with core types like module, function, and None.
  • We'll surely break some external tools that rely on the names of these classes.

I think the case for changing is strongest for the various descriptor types, where it's otherwise difficult right now to map a type to the entry in the types module, and weakest for core builtins like module, function, and NoneType.

Comment thread Doc/library/multiprocessing.rst Outdated Show resolved Hide resolved

Copy link
Copy Markdown
Member

I think the case for changing is strongest for the various descriptor types, where it's otherwise difficult right now to map a type to the entry in the types module, and weakest for core builtins like module, function, and NoneType.

Agreed. I'm broadly supportive of this change, but the change to None in particular makes me feel pretty queasy.

gvanrossum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

I have a few nits -- feel free to address or ignore those.

I'd like @JelleZijlstra or another typeshed/mypy dev to have a quick look here to see whether changing a few names like function, ellipsis would cause any issues for static type checkers (though I doubt it).

>>> mock = MagicMock(async_func)
>>> mock
<MagicMock spec='function' id='...'>
<MagicMock spec='FunctionType' id='...'>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

Why not types.FunctionType?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

Because the MagicMock repr contains only spec_class.__name__, not the fully qualified name.

Comment thread Lib/test/test_json/test_fail.py Outdated
import sys
with self.assertRaisesRegex(TypeError,
'Object of type module is not JSON serializable'):
'Object of type ModuleType is not JSON serializable'):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

Why is this not types.ModuleType?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

The same as above. It outputs type(obj).__name__.

Comment thread Objects/cellobject.c
PyObject *obj = NULL;

if (!_PyArg_NoKeywords("cell", kwargs)) {
if (!_PyArg_NoKeywords("CellType", kwargs)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

It's debatable whether this should have the types. prefix -- what do we do in other similar situations?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

Only short name is used in almost all of other similar situations (over 50 uses of this function, around 1700 uses of all PyArg_* functions). The only known exceptions are array.array, sqlite3.Connection, type.__new__, and deque.rotate.

Copy link
Copy Markdown
Member

I'd like @JelleZijlstra or another typeshed/mypy dev to have a quick look here to see whether changing a few names like function, ellipsis would cause any issues for static type checkers (though I doubt it).

Fully static type checkers like mypy never look at the runtime type objects so it shouldn't matter. Tools like stubtest or my pyanalyze typechecker might need to adapt a little but that's pretty easy.

Copy link
Copy Markdown
Member

Fully static type checkers like mypy never look at the runtime type objects so it shouldn't matter. Tools like stubtest or my pyanalyze typechecker might need to adapt a little but that's pretty easy.

I was thinking about it slightly differently. Typeshed defines classes builtins.function and builtins.ellipsis (and maybe others). Those were always a typeshed-only fiction, but mypy does recognize it -- e.g. this program shows no errors:

def f(): pass func: function = f

There was some precedent for this when type(f) would return <class 'function'> at runtime. But the new regime would suggest that instead the variable func above should be typed as

import types func: types.FunctionType = f

which currently produces an error in mypy.

(In a sense, this PR finally resolves a problem that mypy experienced from its creation: what is the name of the type of a function object. Grepping shows dozens of occurrences of builtins.function in the mypy source code.)

I agree that we shouldn't stop this PR because it exposes mypy's "lie". :-)

Copy link
Copy Markdown
Member

Thanks for the answers, go ahead and merge.

Copy link
Copy Markdown
Member Author

What do you think about omitting the "types." prefix like the "builtins." prefix and, in some cases, the "__main__." prefix, in error messages and type reprs?

Copy link
Copy Markdown
Member

This is going to break things. Changing the names NoneType, the type of generators and functions is going to break someone's code. Maybe they shouldn't be relying on those names and those are acceptable breakages, but I think we should take a bit more care here.

There are a lot of changes to the tests. If this change breaks our tests, it is reasonable to assume it will break other people's tests.

Copy link
Copy Markdown
Member

I'm happy to retract my approval in favor of more discussion -- maybe we should do that on Discourse though, where we can get the view of more people whose code actually will break. I agree that the amount of change needed in our own tests doesn't bode well.

AlexWaygood removed their request for review February 3, 2023 10:12

tungol commented Nov 11, 2024

Copy link
Copy Markdown
Contributor

Just to note: the new-in-3.13 types.CapsuleType / PyCapsule is not included in the current state of this MR, and should also be updated if this gets picked up again.

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 11, 2026

read-the-docs-community Bot commented Jun 4, 2026
edited
Loading

Copy link
Copy Markdown

brettcannon removed their request for review June 4, 2026 18:00
github-actions Bot removed the stale Stale PR or inactive for long period of time. label Jun 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants

Footer

© 2026 GitHub, Inc.