Skip to content

Commit

Permalink
Clean up some more Python 2-isms (#942)
Browse files Browse the repository at this point in the history
- Reword and simplify a few docstrings.
- Add back a test for 2-arg super() in property() definitions that
  was (accidentally?) dropped in #936.

Co-authored-by: Hynek Schlawack <hs@ox.cx>
  • Loading branch information
wbolster and hynek authored Mar 21, 2022
1 parent e18b5f3 commit d785c65
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/glossary.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Glossary
... assert 23 == d.method()

- Slotted classes must implement :meth:`__getstate__ <object.__getstate__>` and :meth:`__setstate__ <object.__setstate__>` to be serializable with `pickle` protocol 0 and 1.
Therefore, ``attrs`` creates these methods automatically for ``slots=True`` classes (Python 2 uses protocol 0 by default).
Therefore, ``attrs`` creates these methods automatically for ``slots=True`` classes.

.. note::

Expand Down
2 changes: 1 addition & 1 deletion docs/hashing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Because according to the definition_ from the official Python docs, the returned
It follows that the moment you (or ``attrs``) change the way equality is handled by implementing ``__eq__`` which is based on attribute values, this constraint is broken.
For that reason Python 3 will make a class that has customized equality unhashable.
Python 2 on the other hand will happily let you shoot your foot off.
Unfortunately ``attrs`` currently mimics Python 2's behavior for backward compatibility reasons if you set ``hash=False``.
Unfortunately, ``attrs`` still mimics (otherwise unsupported) Python 2's behavior for backward compatibility reasons if you set ``hash=False``.

The *correct way* to achieve hashing by id is to set ``@attr.s(eq=False)``.
Setting ``@attr.s(hash=False)`` (which implies ``eq=True``) is almost certainly a *bug*.
Expand Down
4 changes: 0 additions & 4 deletions src/attr/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@


def just_warn(*args, **kw):
"""
We only warn on Python 3 because we are not aware of any concrete
consequences of not setting the cell on Python 2.
"""
warnings.warn(
"Running interpreter doesn't sufficiently support code object "
"introspection. Some features like bare super() or accessing "
Expand Down
9 changes: 4 additions & 5 deletions src/attr/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,10 @@ def matches_re(regex, flags=0, func=None):
:param regex: a regex string or precompiled pattern to match against
:param int flags: flags that will be passed to the underlying re function
(default 0)
:param callable func: which underlying `re` function to call (options
are `re.fullmatch`, `re.search`, `re.match`, default
is ``None`` which means either `re.fullmatch` or an emulation of
it on Python 2). For performance reasons, they won't be used directly
but on a pre-`re.compile`\ ed pattern.
:param callable func: which underlying `re` function to call. Valid options
are `re.fullmatch`, `re.search`, and `re.match`; the default ``None``
means `re.fullmatch`. For performance reasons, the pattern is always
precompiled using `re.compile`.
.. versionadded:: 19.2.0
.. versionchanged:: 21.3.0 *regex* can be a pre-compiled pattern.
Expand Down
10 changes: 9 additions & 1 deletion tests/test_slots.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ def test_getstate_set_state_force_true(self, cls):

def test_slots_super_property_get():
"""
On Python 2/3: the `super(self.__class__, self)` works.
Both `super()` and `super(self.__class__, self)` work.
"""

@attr.s(slots=True)
Expand All @@ -707,8 +707,16 @@ class B(A):
def f(self):
return super().f ** 2

@attr.s(slots=True)
class C(A):
@property
def f(self):
return super(C, self).f ** 2

assert B(11).f == 121
assert B(17).f == 289
assert C(11).f == 121
assert C(17).f == 289


def test_slots_super_property_get_shortcut():
Expand Down

0 comments on commit d785c65

Please sign in to comment.