Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
58b799d
Fix wrong Sphinx markup preventing syntax highlighting
geryogam Mar 17, 2021
f77b0f9
Fix a bug in the ClassMethod Python equivalent
geryogam Mar 17, 2021
1dbd876
Fix the description of the ClassMethod Python equivalent
geryogam Mar 17, 2021
d0d301e
Raise TypeError for __get__(None, None) calls
geryogam Mar 19, 2021
e87fe37
Fix a typo
geryogam Mar 22, 2021
52a06d0
Use the existing cls variable
geryogam Mar 25, 2021
2f9971b
Allow keyword arguments in Object.__new__
geryogam Mar 31, 2021
2f335ca
Use two more super() for consistency
geryogam Mar 31, 2021
4886dbb
Add Object.__getattribute__ to raise AttributeError with the correct …
geryogam Mar 31, 2021
705c577
Allow attribute lookup from a class in Member.__get__
geryogam Mar 31, 2021
c0e6432
Allow keyword arguments in Type.__new__
geryogam Mar 31, 2021
81ec93c
Raise ValueError for slot names conflicting with class variables
geryogam Apr 1, 2021
34eef3a
Update descriptor.rst
geryogam Apr 1, 2021
ae8c622
Raise TypeError for non-empty __slots__ in subclasses of variable-len…
geryogam Apr 5, 2021
1d5fb96
Raise TypeError for non-empty __slots__ in subclasses of variable-len…
geryogam Apr 5, 2021
4671b91
Apply requested changes and remove changes on the slots emulation
geryogam May 10, 2022
0a132bf
Merge branch 'main' into patch-14
geryogam May 10, 2022
62fe4fc
Update descriptor.rst
geryogam May 10, 2022
1252f4b
Update descriptor.rst
geryogam May 10, 2022
58ae977
Update descriptor.rst
geryogam May 10, 2022
5630c64
Update descriptor.rst
geryogam May 10, 2022
bfb33f7
Merge branch 'main' into patch-14
geryogam May 10, 2022
5d45eaa
Revert the two-argument form of super() and forward variadic arguments
geryogam Oct 4, 2022
c853e6b
Merge branch 'main' into patch-14
rhettinger Oct 7, 2022
bcc9da0
Update descriptor.rst
geryogam Oct 8, 2022
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
Raise TypeError for __get__(None, None) calls
  • Loading branch information
geryogam authored Mar 19, 2021
commit d0d301eaf1ccb1b17533e90f430a6e0fbc06e0d4
10 changes: 10 additions & 0 deletions Doc/howto/descriptor.rst
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,8 @@ here is a pure Python equivalent:
self._name = name

def __get__(self, obj, objtype=None):
if obj is None and objtype is None:
raise TypeError('__get__(None, None) is invalid')
if obj is None:
return self
if self.fget is None:
Expand Down Expand Up @@ -1087,6 +1089,8 @@ during dotted lookup from an instance. Here's how it works:

def __get__(self, obj, objtype=None):
"Simulate func_descr_get() in Objects/funcobject.c"
if obj is None and objtype is None:
raise TypeError('__get__(None, None) is invalid')
if obj is None:
return self
return MethodType(self, obj)
Expand Down Expand Up @@ -1214,6 +1218,8 @@ Using the non-data descriptor protocol, a pure Python version of
self.f = f

def __get__(self, obj, objtype=None):
if obj is None and objtype is None:
raise TypeError('__get__(None, None) is invalid')
return self.f


Expand Down Expand Up @@ -1277,6 +1283,8 @@ Using the non-data descriptor protocol, a pure Python version of
self.f = f

def __get__(self, obj, cls=None):
if obj is None and cls is None:
raise TypeError('__get__(None, None) is invalid')
if cls is None:
cls = type(obj)
if hasattr(self.f, '__get__'):
Copy link
Contributor

Choose a reason for hiding this comment

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

Out-of-date. This currently reads:


    class ClassMethod:
        "Emulate PyClassMethod_Type() in Objects/funcobject.c"

        def __init__(self, f):
            self.f = f

        def __get__(self, obj, cls=None):
            if cls is None:
                cls = type(obj)
            if hasattr(type(self.f), '__get__'):
                return self.f.__get__(cls, cls)
            return MethodType(self.f, cls)

Copy link
Contributor Author

@geryogam geryogam May 10, 2022

Choose a reason for hiding this comment

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

Alright. I wish we had a convenient way in Python to bypass instance attributes (with type(self.f), the attribute '__get__' is also looked up in the metaclass). I suggested this on Stack Overflow: https://stackoverflow.com/q/67099920/2326961

Expand Down Expand Up @@ -1432,6 +1440,8 @@ by member descriptors:
def __get__(self, obj, objtype=None):
'Emulate member_get() in Objects/descrobject.c'
# Also see PyMember_GetOne() in Python/structmember.c
if obj is None and objtype is None:
raise TypeError('__get__(None, None) is invalid')
value = obj._slotvalues[self.offset]
if value is null:
raise AttributeError(self.name)
Expand Down