8000 bpo-41287: Handle `doc` argument of `property.__init__` in subclasses by sizmailov · Pull Request #23205 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-41287: Handle doc argument of property.__init__ in subclasses #23205

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

Merged
merged 19 commits into from
May 29, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add test for property copy doc
  • Loading branch information
sizmailov committed May 26, 2022
commit 4943868df7fe79049c9b1e6443e44f993655f5ef
60 changes: 60 additions & 0 deletions Lib/test/test_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,66 @@ def spam(self):
Foo.spam.__doc__,
"spam wrapped in property subclass")

@unittest.skipIf(sys.flags.optimize >= 2,
"Docstrings are omitted with -O2 and above")
def test_docstring_copy2(self):
"""
Property tries to provide the best docstring it finds for it's copies.
If a user-provieded docstring is avaialble it's preserved in the copies.
If no docsting is available during property creation, the property
would utilze docstring from getter if available.
"""
def getter1(self):
return 1
def getter2(self):
"""doc 2"""
return 2
def getter3(self):
"""doc 3"""
return 3

# Case-1: user-provied doc is preserved in copies
# of property with undocumented getter
p = property(getter1, None, None, "doc-A")

p2 = p.getter(getter2)
self.assertEqual(p.__doc__, "doc-A")
self.assertEqual(p2.__doc__, "doc-A")

# Case-2: user-provied doc is preserved in copies
# of property with documented getter
p = property(getter2, None, None, "doc-A")

p2 = p.getter(getter3)
self.assertEqual(p.__doc__, "doc-A")
self.assertEqual(p2.__doc__, "doc-A")

# Case-3: with no user-provied doc new getter doc
# takes precendence
p = property(getter2, None, None, None)

p2 = p.getter(getter3)
self.assertEqual(p.__doc__, "doc 2")
self.assertEqual(p2.__doc__, "doc 3")

# Case-4: A user-provied doc is assigned after property construction
# with documented getter. The doc IS NOT preserved.
# It's an odd behaviour, but it's a strange enough
# use case with no easy solution.
p = property(getter2, None, None, None)
p.__doc__ = "user"
p2 = p.getter(getter3)
self.assertEqual(p.__doc__, "user")
self.assertEqual(p2.__doc__, "doc 3")

# Case-5: A user-provied doc is assigned after property construction
# with UNdocumented getter. The doc IS preserved.
p = property(getter1, None, None, None)
p.__doc__ = "user"
p2 = p.getter(getter2)
self.assertEqual(p.__doc__, "user")
self.assertEqual(p2.__doc__, "user")

@unittest.skipIf(sys.flags.optimize >= 2,
"Docstrings are omitted with -O2 and above")
def test_property_setter_copies_getter_docstring(self):
Expand Down
0