Skip to content

Commit

Permalink
Merge pull request #203 from MichalHaluza/revert_attr_workarounds
Browse files Browse the repository at this point in the history
Revert "Remove workarounds for old version of attrs library"
  • Loading branch information
rohanpm committed Apr 3, 2023
2 parents be2e4f5 + 56e0603 commit edb1e4a
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions pubtools/pulplib/_impl/compat_attr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

# pylint: disable=invalid-name

# Set some defaults when using attr
# Wrappers around attr module to handle some incompatibilities with older versions of the module.


def s(*args, **kwargs):
# A few features we'd like to use are not available on older Python/attrs
# A few features we'd like to use are not available on older attrs
# versions, so we just silently disable them.
#
# Note this means we're technically exporting a different API on
# Py2 vs Py3, i.e. keywords-only isn't enforced on Py2.
# older versions of attr, i.e. keywords-only isn't enforced.
# This isn't ideal, but is mitigated by the fact that any new code
# using this library will surely not be tested solely on Py2.
# using this library will surely not be tested solely using old versions of attr.
kwargs = kwargs.copy()

if "slots" not in kwargs:
Expand All @@ -28,10 +28,27 @@ def s(*args, **kwargs):
# don't use the attrs-generated repr by default
kwargs["repr"] = False

if "kw_only" in kwargs and ATTR_VERSION < (18, 2): # pragma: no cover
# This is only implemented in attrs 18.2 and newer.
# attr.s() will raise if kw_only is provided in older version of attr.
del kwargs["kw_only"]

return attr.s(*args, **kwargs)


fields_dict = attr.fields_dict
if ATTR_VERSION < (18, 1): # pragma: no cover
# older attrs version didn't provide this function yet,
# but it's easily added here.
def fields_dict(cls):
out = {}
for field in attr.fields(cls):
out[field.name] = field
return out

else:
fields_dict = attr.fields_dict


ib = attr.ib
Factory = attr.Factory
fields = attr.fields
Expand Down

0 comments on commit edb1e4a

Please sign in to comment.