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

Fix version checks in SDK #5991

Merged
merged 4 commits into from
Apr 9, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Escaping in the `filter` parameter in generated URLs
(<https://github.com/opencv/cvat/issues/5566>)
- Rotation property lost during saving a mutable attribute (<https://github.com/opencv/cvat/pull/5968>)
- Server micro version support check in SDK/CLI (<https://github.com/opencv/cvat/pull/5991>)

### Security
- TDB
Expand Down
18 changes: 10 additions & 8 deletions cvat-sdk/cvat_sdk/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,25 +253,27 @@ def check_server_version(self, fail_if_unsupported: Optional[bool] = None) -> No
raise IncompatibleVersionException(msg)
return

sdk_version = pv.Version(VERSION)

# We only check base version match. Micro releases and fixes do not affect
# API compatibility in general.
if all(
server_version.base_version != sv.base_version for sv in self.SUPPORTED_SERVER_VERSIONS
if not any(
self._is_version_compatible(server_version, supported_version)
for supported_version in self.SUPPORTED_SERVER_VERSIONS
):
msg = (
"Server version '%s' is not compatible with SDK version '%s'. "
"Some SDK functions may not work properly with this server. "
"You can continue using this SDK, or you can "
"try to update with 'pip install cvat-sdk'."
) % (server_version, sdk_version)
) % (server_version, pv.Version(VERSION))
self.logger.warning(msg)
if fail_if_unsupported:
raise IncompatibleVersionException(msg)

def _is_version_compatible(self, current: pv.Version, target: pv.Version) -> bool:
# Check for (major, minor) compatibility.
# Micro releases and fixes do not affect API compatibility in general.
upper_bound = pv.Version(f"{target.epoch}{target.major}.{target.minor + 1}")
return target <= current < upper_bound
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is slightly wrong, because it will calculate that 2.5.0a1 is compatible with 2.4.0. I would suggest something like this instead:

return current in packaging.specifiers.SpecifierSet(f"~= {target.epoch}!{target.major}.{target.minor}.{target.micro}")

Copy link
Contributor Author

@zhiltsov-max zhiltsov-max Apr 8, 2023

Choose a reason for hiding this comment

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

Yes, it's a nice catch.

Upd: it's better, but there is a problem with this approach, that

Version("2.1") in specifiers.Specifier(f"~= 2.1.pre1") -> True
Version("2.1.1") in specifiers.Specifier(f"~= 2.1.pre1") -> True
Version("2.1.1") in specifiers.Specifier(f"~= 2.1.0") -> True

Maybe it's not a big deal, but it doesn't look expected.

Copy link
Contributor

Choose a reason for hiding this comment

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

Since the version on the right side comes from the SDK, I wouldn't expect it to be a prerelease. And what's wrong with the 3rd case?

Copy link
Contributor Author

@zhiltsov-max zhiltsov-max Apr 8, 2023

Choose a reason for hiding this comment

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

I think, given what we do now (add the next release version to support the public instance, and once we publish it, we update SDK), it makes sense to try to limit the next version to its pre-release variant, so the old SDK will stop working automatically, once the new server release is deployed.

E.g.:
release 2.4
public server: 2.5.devX
sdk: v2.4 supporting >=2.4, <2.5

release 2.5
public server: 2.5
sdk v2.4 stops working


def get_server_version(self) -> pv.Version:
# TODO: allow to use this endpoint unauthorized
(about, _) = self.api_client.server_api.retrieve_about()
return pv.Version(about.version)

Expand Down
43 changes: 43 additions & 0 deletions tests/python/sdk/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,49 @@ def mocked_version(_):
assert "Server version '0' is not compatible with SDK version" in logger_stream.getvalue()


@pytest.mark.parametrize(
"server_version, supported_version",
[
("3.2", "2"),
("2", "2.1"),
("2.1", "2.1"),
("2.1.1", "2.1"),
("2.2", "2.1"),
("2.2", "2.3"),
("2.1.0.dev123", "2.1.post2"),
("1!1.3", "2.1"),
("1!1.1.dev12", "1!1.1"),
SpecLad marked this conversation as resolved.
Show resolved Hide resolved
],
)
def test_can_check_server_version_compatibility(
fxt_logger: Tuple[Logger, io.StringIO],
monkeypatch: pytest.MonkeyPatch,
server_version: str,
supported_version: str,
):
logger, _ = fxt_logger

supported_v = pv.Version(supported_version)
server_v = pv.Version(server_version)

# Currently, it is ~=, as defined in https://peps.python.org/pep-0440/
expected_true = (
SpecLad marked this conversation as resolved.
Show resolved Hide resolved
supported_v
<= server_v
< pv.Version(f"{supported_v.epoch}{supported_v.major}.{supported_v.minor + 1}")
)

monkeypatch.setattr(Client, "get_server_version", lambda _: server_v)
monkeypatch.setattr(Client, "SUPPORTED_SERVER_VERSIONS", [supported_v])
config = Config(allow_unsupported_server=False)

with ExitStack() as es:
if not expected_true:
es.enter_context(pytest.raises(IncompatibleVersionException))

Client(url=BASE_URL, logger=logger, config=config, check_server_version=True)


@pytest.mark.parametrize("verify", [True, False])
def test_can_control_ssl_verification_with_config(verify: bool):
config = Config(verify_ssl=verify)
Expand Down