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

Updates __eq__ implementation on the data types #445

Merged
merged 26 commits into from
Dec 19, 2022

Conversation

Karrenbelt
Copy link
Collaborator

@Karrenbelt Karrenbelt commented Nov 16, 2022

Proposed changes

Updating the implementation of custom __eq__ methods on custom data types: PackageVersion, PublicId, PackageId and Dependency.

as per base PR description: #428
and for reference: googleapis/google-cloud-python#3455

Fixes

If it fixes a bug or resolves a feature request, be sure to link to that issue.

Types of changes

What types of changes does your code introduce to agents-aea?
Put an x in the boxes that apply

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING doc
  • I am making a pull request against the develop branch (left side). Also you should start your branch off our develop.
  • Lint and unit tests pass locally with my changes and CI passes too
  • I have added tests that prove my fix is effective or that my feature works
  • I have checked that code coverage does not decrease.
  • I have added necessary documentation (if appropriate)
  • Any dependent changes have been merged and published in downstream modules

Further comments

If this is a relatively large or complex change, kick off the discussion by explaining why you chose the solution you did and what alternatives you considered, etc...

Comment on lines +89 to +90
__slots__ = ("_version",)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

such that a generic solution could be implemented that is equal for all classes in this module

Comment on lines 650 to 660
def __eq__(self, other: Any) -> bool:
"""Compare with another object."""
return (
isinstance(other, PackageId)
and self.package_type == other.package_type
and self.public_id == other.public_id
)
"""Check equality."""
if not isinstance(other, self.__class__):
return NotImplemented # Delegate comparison to the other instance's __eq__.
return all(getattr(self, s) == getattr(other, s) for s in self.__slots__)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__slots__ = ("_package_type", "_public_id")

Comment on lines 875 to 890
def __eq__(self, other: Any) -> bool:
"""Compare with another object."""
return (
isinstance(other, Dependency)
and self._name == other._name
and self._version == other._version
and self._index == other._index
and self._git == other._git
and self._ref == other._ref
)
"""Check equality."""
if not isinstance(other, self.__class__):
return NotImplemented # Delegate comparison to the other instance's __eq__.
return all(getattr(self, s) == getattr(other, s) for s in self.__slots__)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__slots__ = ("_name", "_version", "_index", "_git", "_ref")

aea/cli/check_packages.py Outdated Show resolved Hide resolved
Comment on lines +458 to +461
"""Check equality."""
if not isinstance(other, self.__class__):
return NotImplemented # Delegate comparison to the other instance's __eq__.
return all(getattr(self, s) == getattr(other, s) for s in self.__slots__[:-1])
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

including the _package_hash will affect the functionality in the following 4 places, which is why I chose not to include it now (it was not included in the original comparison either). We may choose to open an issue on this if we want to change this later.

  • eject

    open-aea/aea/cli/eject.py

    Lines 204 to 206 in 77474ff

    reverse_reachable_dependencies = reachable_nodes(
    reverse_dependencies, {package_id.without_hash()}
    )
  • remove

    open-aea/aea/cli/remove.py

    Lines 193 to 226 in 77474ff

    def get_item_dependencies_with_reverse_dependencies(
    self, item: PackageConfiguration, package_id: Optional[PackageId] = None
    ) -> Dict[PackageId, Set[PackageId]]:
    """
    Get item dependencies.
    It's recursive and provides all the sub dependencies.
    :param item: the item package configuration
    :param package_id: the package id.
    :return: dict with PackageId: and set of PackageIds that uses this package
    """
    result: defaultdict = defaultdict(set)
    for dep_package_id in self._get_item_requirements(
    item, self._ignore_non_vendor
    ):
    if package_id is None:
    _ = result[dep_package_id.without_hash()] # init default dict value
    else:
    result[dep_package_id.without_hash()].add(package_id.without_hash())
    if not self.is_present_in_agent_config(
    dep_package_id.without_hash()
    ): # pragma: nocover
    continue
    dep_item = self.get_item_config(dep_package_id)
    for item_key, deps in self.get_item_dependencies_with_reverse_dependencies(
    dep_item, dep_package_id
    ).items():
    result[item_key.without_hash()] = result[item_key].union(deps)
    return result
  • check_packages
    def _add_package_type(package_type: PackageType, public_id_str: str) -> PackageId:
    return PackageId(package_type, PublicId.from_str(public_id_str))
  • registry/add
    def fetch_package(obj_type: str, public_id: PublicId, cwd: str, dest: str) -> Path:
    """
    Fetch a package (connection/contract/protocol/skill) from Registry.
    :param obj_type: str type of object you want to fetch:
    'connection', 'protocol', 'skill'
    :param public_id: str public ID of object.
    :param cwd: str path to current working directory.
    :param dest: destination where to save package.
    :return: package path
    """
    logger.debug(
    "Fetching {obj_type} {public_id} from Registry...".format(
    public_id=public_id, obj_type=obj_type
    )
    )
    logger.debug(
    "Downloading {obj_type} {public_id}...".format(
    public_id=public_id, obj_type=obj_type
    )
    )
    package_meta = get_package_meta(obj_type, public_id)
    file_url = cast(str, package_meta["file"])
    filepath = download_file(file_url, cwd)
    # next code line is needed because the items are stored in tarball packages as folders
    dest = os.path.split(dest)[0]
    logger.debug(
    "Extracting {obj_type} {public_id}...".format(
    public_id=public_id, obj_type=obj_type
    )
    )
    extract(filepath, dest)
    logger.debug(
    "Successfully fetched {obj_type} '{public_id}'.".format(
    public_id=public_id, obj_type=obj_type
    )
    )
    package_path = os.path.join(dest, public_id.name)
    return Path(package_path)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please open an issue for this and add a comment in code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Karrenbelt
Copy link
Collaborator Author

Karrenbelt commented Nov 17, 2022

No longer failing the following (was so after 3b27e78 - see CI run)

FAILED tests/test_cli/test_eject.py::TestRecursiveEjectIsAborted::test_recursive_eject_commands_non_quiet_negative
FAILED tests/test_cli/test_eject.py::TestEjectWithSymlink::test_command - Val...
FAILED tests/test_cli/test_eject.py::TestEjectWithLatest::test_command - Valu...
FAILED tests/test_cli/test_eject.py::TestRecursiveEject::test_recursive_eject_commands_positive
FAILED tests/test_cli/test_eject.py::TestEjectCommands::test_eject_commands_positive
FAILED tests/test_test_tools/test_test_cases.py::TestAddAndEjectComponent::test_add_and_eject
FAILED tests/test_configurations/test_constants.py::test_signing_protocol_hash
FAILED tests/test_cli/test_registry/test_add.py::TestAddFromHash::test_from_hash
ERROR tests/test_cli/test_eject.py::TestEjectCommandReplacesReferences::test_aea_config_references_updated_correctly
ERROR tests/test_cli/test_eject.py::TestEjectCommandReplacesReferences::test_username_is_correct
ERROR tests/test_cli/test_eject.py::TestEjectCommandReplacesReferences::test_username_is_correct
ERROR tests/test_cli/test_eject.py::TestEjectCommandReplacesCustomConfigurationReference::test_aea_config_references_updated_correctly
ERROR tests/test_cli/test_eject.py::TestEjectCommandReplacesCustomConfigurationReference::test_username_is_correct
ERROR tests/test_cli/test_eject.py::TestEjectCommandReplacesCustomConfigurationReference::test_username_is_correct

all remaining failures are those related to newly introduced tests in base PR #428

FAILED tests/test_configurations/test_data_types.py::test_self_type_comparison[PackageId]
FAILED tests/test_configurations/test_data_types.py::test_type_comparison[PackageId-Dependency]
FAILED tests/test_configurations/test_data_types.py::test_type_comparison[map-PackageId]
FAILED tests/test_configurations/test_data_types.py::test_type_comparison[PublicId-PackageId]
FAILED tests/test_configurations/test_data_types.py::test_type_comparison[int-PackageId]
FAILED tests/test_configurations/test_data_types.py::test_type_comparison[zip-PackageId]
FAILED tests/test_configurations/test_data_types.py::test_type_comparison[list0-PackageId]
FAILED tests/test_configurations/test_data_types.py::test_self_type_comparison[Dependency]
FAILED tests/test_configurations/test_data_types.py::test_type_comparison[PackageId-PublicId]
FAILED tests/test_configurations/test_data_types.py::test_type_comparison[list1-PackageId]
FAILED tests/test_configurations/test_data_types.py::test_type_comparison[str-PackageId]
FAILED tests/test_configurations/test_data_types.py::test_self_type_comparison[PublicId]
FAILED tests/test_configurations/test_data_types.py::test_package_version_comparison
FAILED tests/test_configurations/test_data_types.py::test_package_id_comparison
FAILED tests/test_configurations/test_data_types.py::test_public_id_comparison
FAILED tests/test_configurations/test_data_types.py::test_type_comparison[tuple-PackageId]
FAILED tests/test_configurations/test_data_types.py::test_type_comparison[Dependency-PackageId]
FAILED tests/test_configurations/test_data_types.py::test_type_comparison[complex-PackageId]
FAILED tests/test_configurations/test_data_types.py::test_type_comparison[float-PackageId]

@angrybayblade angrybayblade changed the title Fix/data types __eq__ [WIP 1.25.0] Updates __eq__ implementation on the data types Nov 17, 2022
@Karrenbelt Karrenbelt changed the title [WIP 1.25.0] Updates __eq__ implementation on the data types [1.25.0] Updates __eq__ implementation on the data types Nov 17, 2022
@Karrenbelt Karrenbelt changed the title [1.25.0] Updates __eq__ implementation on the data types Updates __eq__ implementation on the data types Nov 28, 2022
@angrybayblade angrybayblade marked this pull request as draft December 14, 2022 14:27
@codecov-commenter
Copy link

Codecov Report

❗ No coverage uploaded for pull request base (tests/test_data_types@d40bcf2). Click here to learn what that means.
Patch has no changes to coverable lines.

Additional details and impacted files
@@                   Coverage Diff                    @@
##             tests/test_data_types     #445   +/-   ##
========================================================
  Coverage                         ?   97.31%           
========================================================
  Files                            ?      221           
  Lines                            ?    19540           
  Branches                         ?        0           
========================================================
  Hits                             ?    19016           
  Misses                           ?      524           
  Partials                         ?        0           
Flag Coverage Δ
unittests 97.31% <0.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@Karrenbelt Karrenbelt marked this pull request as ready for review December 18, 2022 16:38
Copy link
Collaborator

@solarw solarw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

…nLike

Fix/delegate `__lt__` to `PackageVersionLike`
Add deflection to custom `__lt__` and add `functools.total_ordering`
@DavidMinarsch DavidMinarsch merged commit 4a8fa40 into tests/test_data_types Dec 19, 2022
@DavidMinarsch DavidMinarsch deleted the fix/data_types__eq__ branch December 22, 2022 22:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants