Skip to content

bpo-46162: make property generic #30238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Lib/test/ann_module7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Test that generic `property` works with future import.

from __future__ import annotations

p: property[int, str] = property()
4 changes: 3 additions & 1 deletion Lib/test/test_genericalias.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ class BaseTest(unittest.TestCase):
WeakSet, ReferenceType, ref,
ShareableList, MPSimpleQueue,
Future, _WorkItem,
Morsel]
Morsel,
property,
]
if ctypes is not None:
generic_types.extend((ctypes.Array, ctypes.LibraryLoader))

Expand Down
78 changes: 78 additions & 0 deletions Lib/test/test_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import sys
import unittest
from test import support
from types import GenericAlias
from typing import TypeVar, get_type_hints

class PropertyBase(Exception):
pass
Expand Down Expand Up @@ -215,6 +217,82 @@ def test_property_set_name_incorrect_args(self):
p.__set_name__(*([0] * i))


class Getter:
@property
def a(self) -> int:
pass

class GetterSetter:
@property
def a(self) -> int:
pass
@a.setter
def a(self, arg: str) -> None:
pass

class GetterSetterDeleter:
@property
def a(self) -> int:
pass
@a.setter
def a(self, arg: str) -> None:
pass
@a.deleter
def a(self) -> None:
pass


class GenericPropertyTests(unittest.TestCase):
def test_property___class_getitem__(self):
p = property[int, str]
self.assertIsInstance(p, GenericAlias)
self.assertIs(p.__origin__, property)
self.assertEqual(p.__args__, (int, str))
self.assertEqual(p.__parameters__, ())

G = TypeVar('G')
S = TypeVar('S')
p1 = property[G, S]
self.assertEqual(p1.__args__, (G, S))
self.assertEqual(p1.__parameters__, (G, S))

# The number of type arguments is not limited:
p2 = property[int, str, bool]
self.assertEqual(p2.__args__, (int, str, bool))
p3 = property[str]
self.assertEqual(p3.__args__, (str,))

def test_property_and_get_type_hints(self):
# Properties should not be present in `get_type_hints`
# or `__annotations__` of a class.
for klass in (Getter, GetterSetter, GetterSetterDeleter):
with self.subTest(klass=klass):
self.assertEqual(get_type_hints(klass), {})
self.assertEqual(klass.__annotations__, {})
# Getting hints of `property` itself is not allowed
with self.assertRaises(TypeError):
get_type_hints(klass.a)

def test_property_inner_annotations(self):
from inspect import getattr_static

p1 = getattr_static(Getter, 'a')
self.assertEqual(get_type_hints(p1.fget), {'return': int})

p2 = getattr_static(GetterSetter, 'a')
self.assertEqual(get_type_hints(p2.fget), {'return': int})
self.assertEqual(get_type_hints(p2.fset), {'arg': str, 'return': type(None)})

p3 = getattr_static(GetterSetterDeleter, 'a')
self.assertEqual(get_type_hints(p3.fget), {'return': int})
self.assertEqual(get_type_hints(p3.fset), {'arg': str, 'return': type(None)})
self.assertEqual(get_type_hints(p3.fdel), {'return': type(None)})

def test_property_and_future_import(self):
from test import ann_module7
self.assertEqual(get_type_hints(ann_module7), {'p': property[int, str]})


# Issue 5890: subclasses of property do not preserve method __doc__ strings
class PropertySub(property):
"""This is a subclass of property"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make :class:`property` generic.
Copy link
Member

Choose a reason for hiding this comment

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

generic can be a bit vague (or generic 😉), could you rephrase this news snippet and the PR title to clarify this is about typing generic alias?

1 change: 1 addition & 0 deletions Objects/descrobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1566,6 +1566,7 @@ static PyMethodDef property_methods[] = {
{"setter", property_setter, METH_O, setter_doc},
{"deleter", property_deleter, METH_O, deleter_doc},
{"__set_name__", property_set_name, METH_VARARGS, set_name_doc},
{"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
{0}
};

Expand Down