You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
pathlib.PurePath.__class_getitem__ does not return GenericAlias #90641
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2022-02-03.09:24:10.997>created_at=<Date2022-01-23.08:16:30.578>labels= ['type-bug', 'library', '3.11']
title='`pathlib.PurePath.__class_getitem__` does not return `GenericAlias`'updated_at=<Date2022-02-11.21:04:05.289>user='https://github.com/sobolevn'
After reviewing #30777 I had a chance to look through other definitions of def __class_getitem__. And I found that the only one left is: pathlib.PurePath.__class_getitem__
All other definitions already have this form: __class_getitem__ = classmethod(GenericAlias).
I don't think that there's anything special about PurePath in this regard. So, I propose to make __class_getitem__ to return GenericAlias as all other types do.
Why __class_getitem__ was added in PurePath at first place? PurePath should not be a generic class, unlike to os.PathLike. For os.PathLike the type parameters represent the returning type of os.fspath() (either str or bytes), but the pathlib module only supports paths as strings, so no parametrization is needed.
I think PurePath.__class_getitem__ should be removed.
It looks like __class_getitem__ was added to PurePath at @asvetlov's request in PR 17498. But, I'm also not quite sure why. The class is not generic in typeshed, so type-checkers will raise an error if you do PurePath[str], even if it is possible to do so at runtime.
New changeset a7c1cc4 by Miss Islington (bot) in branch '3.9': bpo-46483: [doc] pathlib classes no longer support parameterized generics (GH-31281) a7c1cc4
New changeset f2fbfbe by Miss Islington (bot) in branch '3.10': bpo-46483: [doc] pathlib classes no longer support parameterized generics (GH-31281) f2fbfbe
Activity
sobolevn commentedon Jan 23, 2022
After reviewing #30777 I had a chance to look through other definitions of
def __class_getitem__. And I found that the only one left is:pathlib.PurePath.__class_getitem__All other definitions already have this form:
__class_getitem__ = classmethod(GenericAlias).I don't think that there's anything special about
PurePathin this regard. So, I propose to make__class_getitem__to returnGenericAliasas all other types do.Initial PR: #17498
PR is on its way.
isidentical commentedon Jan 23, 2022
New changeset 1f715d5 by Nikita Sobolev in branch 'main':
bpo-46483: change
PurePath.__class_getitem__to returnGenericAlias(GH-30822)1f715d5
serhiy-storchaka commentedon Jan 23, 2022
Why __class_getitem__ was added in PurePath at first place? PurePath should not be a generic class, unlike to os.PathLike. For os.PathLike the type parameters represent the returning type of os.fspath() (either str or bytes), but the pathlib module only supports paths as strings, so no parametrization is needed.
I think PurePath.__class_getitem__ should be removed.
AlexWaygood commentedon Jan 23, 2022
It looks like __class_getitem__ was added to PurePath at @asvetlov's request in PR 17498. But, I'm also not quite sure why. The class is not generic in typeshed, so type-checkers will raise an error if you do PurePath[str], even if it is possible to do so at runtime.
Typeshed stub for PurePath: https://github.com/python/typeshed/blob/dff461fc6adef1ec3a4b58af5abdf0cdebf2429a/stdlib/pathlib.pyi#L20
asvetlov commentedon Jan 24, 2022
IIRC it was added to reflect the presence of __class_getitem__ method in typeshed.
Please feel free to drop it the method is not really needed.
11 remaining items
AlexWaygood commentedon Jan 30, 2022
The desmper121 user appears to be a spammer; I am undoing all the changes they made to this BPO ticket.
serhiy-storchaka commentedon Feb 3, 2022
New changeset 7ffe7ba by Nikita Sobolev in branch 'main':
bpo-46483: Remove
__class_getitem__frompathlib.PurePath(GH-30848)7ffe7ba
serhiy-storchaka commentedon Feb 11, 2022
New changeset e0bc8ee by Alex Waygood in branch 'main':
bpo-46483: [doc] pathlib classes no longer support parameterized generics (GH-31281)
e0bc8ee
miss-islington commentedon Feb 11, 2022
New changeset a7c1cc4 by Miss Islington (bot) in branch '3.9':
bpo-46483: [doc] pathlib classes no longer support parameterized generics (GH-31281)
a7c1cc4
miss-islington commentedon Feb 11, 2022
New changeset f2fbfbe by Miss Islington (bot) in branch '3.10':
bpo-46483: [doc] pathlib classes no longer support parameterized generics (GH-31281)
f2fbfbe