Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#253 - Do not emit warnings during merge #255

Merged
merged 1 commit into from
Mar 25, 2022
Merged
Changes from all commits
Commits
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
23 changes: 12 additions & 11 deletions cassis/typesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,12 +561,13 @@ def get_feature(self, name: str) -> Optional[Feature]:
else:
return None

def _add_feature(self, feature: Feature, inherited: bool = False):
def _add_feature(self, feature: Feature, inherited: bool = False, warn: bool = True):
"""Add the given feature to his type.

Args:
feature: The feature
inherited: Indicates whether this feature is inherited from a parent or not
warn: Emit a user warning when exactly redefining features

"""
# Clear the feature cache when adding a new feature. Note that this method is also called by supertypes when
Expand All @@ -578,26 +579,26 @@ def _add_feature(self, feature: Feature, inherited: bool = False):
if feature.name in target:
redefined_feature = target[feature.name]

if redefined_feature == feature:
msg = f"Feature with name [{feature.name}] already exists in [{self.name}]!"
warnings.warn(msg)
else:
if redefined_feature != feature:
msg = "Feature with name [{}] already exists in [{}] but is redefined differently!".format(
feature.name, self.name
)
raise ValueError(msg)
elif warn:
msg = f"Feature with name [{feature.name}] already exists in [{self.name}]!"
warnings.warn(msg)
return

# Check that feature is not redefined on parent type
if feature.name in self._inherited_features:
redefined_feature = self._inherited_features[feature.name]

if redefined_feature == feature:
msg = f"Feature with name [{feature.name}] already exists in parent!"
warnings.warn(msg)
else:
if redefined_feature != feature:
msg = f"Feature with name [{feature.name}] already exists in parent but is redefined!"
raise ValueError(msg)
elif warn:
msg = f"Feature with name [{feature.name}] already exists in parent!"
warnings.warn(msg)
return

target[feature.name] = feature
Expand Down Expand Up @@ -1423,7 +1424,7 @@ def merge_typesystems(*typesystems: TypeSystem) -> TypeSystem:
)

for feature in t.features:
created_type._add_feature(feature)
created_type._add_feature(feature, warn=False)
else:
# Type is already defined
existing_type = merged_ts.get_type(t.name)
Expand All @@ -1447,7 +1448,7 @@ def merge_typesystems(*typesystems: TypeSystem) -> TypeSystem:

# If the type is already defined, merge features
for feature in t.features:
existing_type._add_feature(feature)
existing_type._add_feature(feature, warn=False)

merged_types.add(t.name)
updated_type_list.remove(t)
Expand Down