-
Notifications
You must be signed in to change notification settings - Fork 16
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
[1.27.0] Tests for custom data types #428
Conversation
[mypy-hypothesis.*] | ||
ignore_missing_imports = True | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
equal to setup in open-autonomy setup.cfg
Codecov ReportBase: 90.29% // Head: 90.29% // Increases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## main #428 +/- ##
=======================================
Coverage 90.29% 90.29%
=======================================
Files 352 352
Lines 28700 28721 +21
=======================================
+ Hits 25916 25935 +19
- Misses 2784 2786 +2
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. |
s = st.fixed_dictionaries( | ||
dict( | ||
author=st.from_type(SimpleId), | ||
name=st.from_type(SimpleId), | ||
version=st.from_type(semver.VersionInfo), | ||
package_hash=st.from_type(IPFSHash), | ||
) | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not all mypy types are covered in the strategies listed here. E.g. in case of SimpleIdOrStr: Union[SimpleId, str]
, a string should adhere to the pattern of SimpleId
or will otherwise be invalid.
for f in funcs: | ||
with pytest.raises((TypeError, ValueError)): | ||
assert f(self, other) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some raise custom ValueError
, I suggest raise TypeError
on all instead.
@given(st.tuples(package_id_strategy, package_id_strategy)) | ||
def test_package_id_comparison(package_id_pair): | ||
"""Test package id comparison""" | ||
|
||
package_id_pair[0]._public_id = package_id_pair[1]._public_id | ||
package_id_pair[0]._package_type = package_id_pair[1]._package_type | ||
version_pair = tuple(p.public_id.package_version._version for p in package_id_pair) | ||
assert all_comparisons_operations_equal(version_pair, package_id_pair) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems to me the desired type of behaviour.
positive_integer_strategy = st.integers(min_value=0) | ||
user_string_pattern = re.compile(r"[a-zA-Z_][a-zA-Z0-9_]{0,127}") | ||
user_string_strategy = st.from_regex(user_string_pattern, fullmatch=True) | ||
simple_id_strategy = st.from_regex(SimpleId.REGEX, fullmatch=True) | ||
ipfs_hash_strategy = st.from_regex(IPFSHash.REGEX, fullmatch=True) | ||
pypi_package_name_strategy = st.from_regex(PyPIPackageName.REGEX, fullmatch=True) | ||
gitref_strategy = st.from_regex(GitRef.REGEX, fullmatch=True) | ||
specifier_strategy = st.from_regex(Specifier._regex, fullmatch=True) | ||
|
||
|
||
st.register_type_strategy(collections.UserString, user_string_strategy) | ||
st.register_type_strategy(SimpleId, simple_id_strategy) | ||
st.register_type_strategy(IPFSHash, ipfs_hash_strategy) | ||
st.register_type_strategy(PyPIPackageName, pypi_package_name_strategy) | ||
st.register_type_strategy(GitRef, gitref_strategy) | ||
st.register_type_strategy(Specifier, specifier_strategy) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, let's stack a fix PR.
TODO: introduce |
This reverts commit cdbb001.
ec365ef
to
d40bcf2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
coool!
…nLike Fix/delegate `__lt__` to `PackageVersionLike`
Add deflection to custom `__lt__` and add `functools.total_ordering`
Updates `__eq__` implementation on the data types
…test Fix: comparison `PackageVersion` `"any"` & `"latest"`
@property | ||
def is_any(self) -> bool: | ||
"""Check whether the version is 'any'.""" | ||
return isinstance(self._version, str) and self._version == "any" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check for instance is done cause will fail on Version() == str()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Proposed changes
Tests that showcase issues with custom data types that implement their own
__eq__
and__lt__
methods.In furtherance of deterministic config serialization - #359
There are several issues with the implementation of
__dunder__
methods in our code base. Here we showcase those with respect to comparison operators:__eq__
,__ne__
,__lt__
,__le__
,__gt__
,__ge__
.open-aea/aea/configurations/data_types.py
Lines 119 to 128 in 5aa7427
open-aea/aea/configurations/data_types.py
Lines 462 to 493 in 5aa7427
open-aea/aea/configurations/data_types.py
Lines 657 to 659 in 5aa7427
Issues
__lt__
.Use
VersionInfo
for everything relating to comparisons inPackageVersion
,PublicId
andPackageId
. It has many useful features implemented for us.functools.total_ordering
missing onPublicId
andPackageId
.False
ifother
is not an instance oftype(self)
in__eq__
and__lt__
operators. We should opt to raise when comparing apples to oranges.PackageVersion.__lt__
andPublicId.__lt__
, but not inPackageId.__lt__
.__eq__
methods.PackageId.__lt__
is most problematic. Not only does it compare the string representation ofPublicId
, containing a string representation of the version, but it also compares against the string representation of any arbitrary object. It should raise an error. We should deal with sorting necessary for serialization in the serialization process.Suggested changes:
semver.VersionInfo
.__eq__
and__lt__
must first check for "any" and "latest" in__lt__
:if self.version_like == other.version_like: return False else self.version_like < other.version_like
PublicId.version
should be a property returning an instance ofsemver.VersionInfo
__eq__
should deflect when notisinstance(other, type(self))
by returningNotImplemented
(see #383).PackageVersion.__lt__
should be / rely onPackageVersion._version
(which should be an instance ofsemver.VersionInfo
)PublicId.__lt__
should raise on incoherenttype
, author or name, otherwise delegate tosemver.VersionInfo
PackageId.__lt__
should raise on incoherenttype
or package_type, otherwise delegate toPublicId
functools.total_ordering
decorator toPublicId
andPackageId
.json
.PackageVersion.json: return self.version_like.to_tuple()
# or a nasty "any" / "latest" stringPublicId.json: return dict(author=..., name=..., version=self.package_version.json, package_hash=...)
PackageId.json: return dict(package_type=..., **self.public_id.json)
This would constitute a breaking change. The usage of "any" and "latest" complicates matters unnecessarily, we may consider changing this prior to next major release.
Types of changes
What types of changes does your code introduce to agents-aea?
Put an
x
in the boxes that applyChecklist
Put an
x
in the boxes that apply.develop
branch (left side). Also you should start your branch off ourdevelop
.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...