mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-15 10:07:06 +08:00
Use a frozenset in context sets and make it comparable/hashable
This commit is contained in:
@@ -13,18 +13,25 @@ class BaseContext(object):
|
|||||||
|
|
||||||
class BaseContextSet(object):
|
class BaseContextSet(object):
|
||||||
def __init__(self, *args):
|
def __init__(self, *args):
|
||||||
self._set = set(args)
|
for arg in args:
|
||||||
|
assert not isinstance(arg, BaseContextSet)
|
||||||
|
self._set = frozenset(args)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_iterable(cls, iterable):
|
def from_iterable(cls, iterable):
|
||||||
return cls.from_set(set(iterable))
|
return cls.from_set(set(iterable))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_set(cls, set_):
|
def _from_frozen_set(cls, frozenset_):
|
||||||
self = cls()
|
self = cls.__new__(cls)
|
||||||
self._set = set_
|
self._set = frozenset_
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
# TODO remove this function
|
||||||
|
@classmethod
|
||||||
|
def from_set(cls, set_):
|
||||||
|
return cls(*set_)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_sets(cls, sets):
|
def from_sets(cls, sets):
|
||||||
"""
|
"""
|
||||||
@@ -35,14 +42,14 @@ class BaseContextSet(object):
|
|||||||
if isinstance(set_, BaseContextSet):
|
if isinstance(set_, BaseContextSet):
|
||||||
aggregated |= set_._set
|
aggregated |= set_._set
|
||||||
else:
|
else:
|
||||||
aggregated |= set_
|
aggregated |= frozenset(set_)
|
||||||
return cls.from_set(aggregated)
|
return cls._from_frozen_set(aggregated)
|
||||||
|
|
||||||
def __or__(self, other):
|
def __or__(self, other):
|
||||||
return self.from_set(self._set | other._set)
|
return self._from_frozen_set(self._set | other._set)
|
||||||
|
|
||||||
def __and__(self, other):
|
def __and__(self, other):
|
||||||
return self.from_set(self._set & other._set)
|
return self._from_frozen_set(self._set & other._set)
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
for element in self._set:
|
for element in self._set:
|
||||||
@@ -67,3 +74,9 @@ class BaseContextSet(object):
|
|||||||
for context in self._set
|
for context in self._set
|
||||||
)
|
)
|
||||||
return mapper
|
return mapper
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
return self._set == other._set
|
||||||
|
|
||||||
|
def __hash__(self):
|
||||||
|
return hash(self._set)
|
||||||
|
|||||||
Reference in New Issue
Block a user