-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Add DriverInfo class for upstream driver tracking #3880
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
Open
vchomakov
wants to merge
4
commits into
redis:master
Choose a base branch
from
vchomakov:feature/driver-info
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+211
−2
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a2c049f
Add DriverInfo class for upstream driver tracking
vchomakov 51d2f60
Fix linting issues - replace tabs with spaces and sort imports
vchomakov de5957c
Fix code formatting in test_asyncio/test_commands.py
vchomakov 8ff83dc
Merge branch 'master' into feature/driver-info
vchomakov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass, field | ||
| from typing import List | ||
|
|
||
| _BRACES = {"(", ")", "[", "]", "{", "}"} | ||
|
|
||
|
|
||
| def _validate_no_invalid_chars(value: str, field_name: str) -> None: | ||
| """Ensure value contains only printable ASCII without spaces or braces. | ||
|
|
||
| This mirrors the constraints enforced by other Redis clients for values that | ||
| will appear in CLIENT LIST / CLIENT INFO output. | ||
| """ | ||
|
|
||
| for ch in value: | ||
| # printable ASCII without space: '!' (0x21) to '~' (0x7E) | ||
| if ord(ch) < 0x21 or ord(ch) > 0x7E or ch in _BRACES: | ||
| raise ValueError( | ||
| f"{field_name} must not contain spaces, newlines, non-printable characters, or braces" | ||
| ) | ||
|
|
||
|
|
||
| def _validate_driver_name(name: str) -> None: | ||
| """Validate an upstream driver name. | ||
|
|
||
| The name should look like a typical Python distribution or package name, | ||
| following a simplified form of PEP 503 normalisation rules: | ||
|
|
||
| * start with a lowercase ASCII letter | ||
| * contain only lowercase letters, digits, hyphens and underscores | ||
|
|
||
| Examples of valid names: ``"django-redis"``, ``"celery"``, ``"rq"``. | ||
| """ | ||
|
|
||
| import re | ||
|
|
||
| _validate_no_invalid_chars(name, "Driver name") | ||
| if not re.match(r"^[a-z][a-z0-9_-]*$", name): | ||
| raise ValueError( | ||
| "Upstream driver name must use a Python package-style name: " | ||
| "start with a lowercase letter and contain only lowercase letters, " | ||
| "digits, hyphens, and underscores (e.g., 'django-redis')." | ||
| ) | ||
|
|
||
|
|
||
| def _validate_driver_version(version: str) -> None: | ||
| _validate_no_invalid_chars(version, "Driver version") | ||
|
|
||
|
|
||
| def _format_driver_entry(driver_name: str, driver_version: str) -> str: | ||
| return f"{driver_name}_v{driver_version}" | ||
|
|
||
|
|
||
| @dataclass | ||
| class DriverInfo: | ||
| """Driver information used to build the CLIENT SETINFO LIB-NAME value. | ||
|
|
||
| The formatted name follows the pattern:: | ||
|
|
||
| name(driver1_vVersion1;driver2_vVersion2) | ||
|
|
||
| Examples | ||
| -------- | ||
| >>> info = DriverInfo() | ||
| >>> info.formatted_name | ||
| 'redis-py' | ||
|
|
||
| >>> info = DriverInfo().add_upstream_driver("django-redis", "5.4.0") | ||
| >>> info.formatted_name | ||
| 'redis-py(django-redis_v5.4.0)' | ||
| """ | ||
|
|
||
| name: str = "redis-py" | ||
| _upstream: List[str] = field(default_factory=list) | ||
|
|
||
| @property | ||
| def upstream_drivers(self) -> List[str]: | ||
| """Return a copy of the upstream driver entries. | ||
|
|
||
| Each entry is in the form ``"driver-name_vversion"``. | ||
| """ | ||
|
|
||
| return list(self._upstream) | ||
|
|
||
| def add_upstream_driver( | ||
| self, driver_name: str, driver_version: str | ||
| ) -> "DriverInfo": | ||
| """Add an upstream driver to this instance and return self. | ||
|
|
||
| The most recently added driver appears first in :pyattr:`formatted_name`. | ||
| """ | ||
|
|
||
| if driver_name is None: | ||
| raise ValueError("Driver name must not be None") | ||
| if driver_version is None: | ||
| raise ValueError("Driver version must not be None") | ||
|
|
||
| _validate_driver_name(driver_name) | ||
| _validate_driver_version(driver_version) | ||
|
|
||
| entry = _format_driver_entry(driver_name, driver_version) | ||
| # insert at the beginning so latest is first | ||
| self._upstream.insert(0, entry) | ||
| return self | ||
|
|
||
| @property | ||
| def formatted_name(self) -> str: | ||
| """Return the base name with upstream drivers encoded, if any. | ||
|
|
||
| With no upstream drivers, this is just :pyattr:`name`. Otherwise:: | ||
|
|
||
| name(driver1_vX;driver2_vY) | ||
| """ | ||
|
|
||
| if not self._upstream: | ||
| return self.name | ||
| return f"{self.name}({';'.join(self._upstream)})" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import pytest | ||
|
|
||
| from redis.driver_info import DriverInfo | ||
|
|
||
|
|
||
| def test_driver_info_default_name_no_upstream(): | ||
| info = DriverInfo() | ||
| assert info.formatted_name == "redis-py" | ||
| assert info.upstream_drivers == [] | ||
|
|
||
|
|
||
| def test_driver_info_single_upstream(): | ||
| info = DriverInfo().add_upstream_driver("django-redis", "5.4.0") | ||
| assert info.formatted_name == "redis-py(django-redis_v5.4.0)" | ||
|
|
||
|
|
||
| def test_driver_info_multiple_upstreams_latest_first(): | ||
| info = DriverInfo() | ||
| info.add_upstream_driver("django-redis", "5.4.0") | ||
| info.add_upstream_driver("celery", "5.4.1") | ||
| assert info.formatted_name == "redis-py(celery_v5.4.1;django-redis_v5.4.0)" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "name", | ||
| [ | ||
| "DjangoRedis", # must start with lowercase | ||
| "django redis", # spaces not allowed | ||
| "django{redis}", # braces not allowed | ||
| "django:redis", # ':' not allowed by validation regex | ||
| ], | ||
| ) | ||
| def test_driver_info_invalid_name(name): | ||
| info = DriverInfo() | ||
| with pytest.raises(ValueError): | ||
| info.add_upstream_driver(name, "3.2.0") | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "version", | ||
| [ | ||
| "3.2.0 beta", # space not allowed | ||
| "3.2.0)", # brace not allowed | ||
| "3.2.0\n", # newline not allowed | ||
| ], | ||
| ) | ||
| def test_driver_info_invalid_version(version): | ||
| info = DriverInfo() | ||
| with pytest.raises(ValueError): | ||
| info.add_upstream_driver("django-redis", version) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The current implementation causes to have lib_name to be ignored - it will be valuable addition to the details in the docstrings.