Skip to content

Commit

Permalink
Remove inaccurate __eq__ implementations.
Browse files Browse the repository at this point in the history
  • Loading branch information
stuhood committed Jul 2, 2018
1 parent 8e0e2b7 commit c1cafb1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 63 deletions.
8 changes: 0 additions & 8 deletions src/python/pants/engine/build_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,6 @@ class UnhydratedStruct(datatype(['address', 'struct', 'dependencies'])):
the `dependencies` field of StructWithDeps.
"""

def __eq__(self, other):
if type(self) != type(other):
return NotImplemented
return self.struct == other.struct

def __ne__(self, other):
return not (self == other)

def __hash__(self):
return hash(self.struct)

Expand Down
8 changes: 0 additions & 8 deletions src/python/pants/engine/legacy/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,6 @@ class HydratedTarget(datatype(['address', 'adaptor', 'dependencies'])):
def addresses(self):
return self.dependencies

def __eq__(self, other):
if type(self) != type(other):
return False
return self.address == other.address

def __ne__(self, other):
return not (self == other)

def __hash__(self):
return hash(self.address)

Expand Down
63 changes: 28 additions & 35 deletions src/python/pants/engine/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,6 @@ def addressables(self):
for name, (path, obj) in self.objects_by_name.items()
}

def __eq__(self, other):
if not type(other) == type(self):
return NotImplemented
return self.namespace == other.namespace

def __ne__(self, other):
return not (self == other)

def __hash__(self):
return hash(self.namespace)

Expand All @@ -159,15 +151,21 @@ class ResolveError(MappingError):
"""Indicates an error resolving targets."""


class AddressMapper(object):
class AddressMapper(datatype([
'parser',
'build_patterns',
'build_ignore_patterns',
'exclude_target_regexps',
'subproject_roots',
])):
"""Configuration to parse build files matching a filename pattern."""

def __init__(self,
parser,
build_patterns=None,
build_ignore_patterns=None,
exclude_target_regexps=None,
subproject_roots=None):
def __new__(cls,
parser,
build_patterns=None,
build_ignore_patterns=None,
exclude_target_regexps=None,
subproject_roots=None):
"""Create an AddressMapper.
Both the set of files that define a mappable BUILD files and the parser used to parse those
Expand All @@ -180,27 +178,22 @@ def __init__(self,
:param list build_ignore_patterns: A list of path ignore patterns used when searching for BUILD files.
:param list exclude_target_regexps: A list of regular expressions for excluding targets.
"""
self.parser = parser
self.build_patterns = tuple(build_patterns or [b'BUILD', b'BUILD.*'])
self.build_ignore_patterns = tuple(build_ignore_patterns or [])
self._exclude_target_regexps = exclude_target_regexps or []
self.exclude_patterns = [re.compile(pattern) for pattern in self._exclude_target_regexps]
self.subproject_roots = subproject_roots or []

def __eq__(self, other):
if self is other:
return True
if type(other) != type(self):
return NotImplemented
return (other.build_patterns == self.build_patterns and
other.parser == self.parser)

def __ne__(self, other):
return not (self == other)
build_patterns = tuple(build_patterns or [b'BUILD', b'BUILD.*'])
build_ignore_patterns = tuple(build_ignore_patterns or [])
exclude_target_regexps = tuple(exclude_target_regexps or [])
subproject_roots = tuple(subproject_roots or [])
return super(AddressMapper, cls).__new__(
cls,
parser,
build_patterns,
build_ignore_patterns,
exclude_target_regexps,
subproject_roots
)

def __hash__(self):
# Compiled regexes are not hashable.
return hash(self.parser)
@memoized_property
def exclude_patterns(self):
return tuple(re.compile(pattern) for pattern in self.exclude_target_regexps)

def __repr__(self):
return 'AddressMapper(parser={}, build_patterns={})'.format(self.parser, self.build_patterns)
Expand Down
21 changes: 9 additions & 12 deletions src/python/pants/engine/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,18 +260,15 @@ def __getattr__(self, item):
return object.__getattribute__(self, item)

def _key(self):
if self.address:
return self.address
else:
def hashable(value):
if isinstance(value, dict):
return tuple(sorted((k, hashable(v)) for k, v in value.items()))
elif isinstance(value, list):
return tuple(hashable(v) for v in value)
else:
return value
return tuple(sorted((k, hashable(v)) for k, v in self._kwargs.items()
if k not in self._INHERITANCE_FIELDS))
def hashable(value):
if isinstance(value, dict):
return tuple(sorted((k, hashable(v)) for k, v in value.items()))
elif isinstance(value, list):
return tuple(hashable(v) for v in value)
else:
return value
return tuple(sorted((k, hashable(v)) for k, v in self._kwargs.items()
if k not in self._INHERITANCE_FIELDS))

def __hash__(self):
return hash(self._key())
Expand Down

0 comments on commit c1cafb1

Please sign in to comment.