KEMBAR78
Add `@final` to many unsubclassable stdlib classes by AlexWaygood · Pull Request #6299 · python/typeshed · GitHub
Skip to content

Conversation

@AlexWaygood
Copy link
Member

These were all discovered using the following extremely crude script, the output of which I then manually checked:

import sys
from inspect import isclass
from enum import EnumMeta

unsubclassables = set()
for modname in sys.modules:
    mod = __import__(modname)
    for thingname in dir(mod):
        thing = getattr(mod, thingname)
        if isclass(thing) and not isinstance(thing, EnumMeta):
            try:
                class Foo(thing): pass
            except TypeError:
                if thing not in unsubclassables:
                    print(modname, thingname)
                    unsubclassables.add(thing)

The only results that this script produced that I have not added @final to are dict_keys, dict_values and dict_items (exposed in collections.abc at runtime, and in builtins in typeshed). I couldn't add @final to these classes because of the following weirdness:

>>> from collections import OrderedDict
>>> dict_keys = type({}.keys())
>>> ordered_dict_keys = type(OrderedDict().keys())
>>> class Foo(dict_keys): pass
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: type 'dict_keys' is not an acceptable base type
>>> issubclass(ordered_dict_keys, dict_keys)
True

So, while these classes are not subclassable at runtime, they nonetheless have subclasses in the stdlib, meaning mypy understandably complains if I try to add @final to them.

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants