- Notifications
You must be signed in to change notification settings - Fork 91
feat: provide and use Python version support check #832
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
base: main
Are you sure you want to change the base?
Conversation
Note that I've also made the Python 3.9 support window as conservative (short) as possible. We can extend it if that's the outcome of our discussions, but it's better to start restrictively and then extend than to start permissively and then contract. |
2139b90
to 4d49aa0
Compare d727f14
to 17ef4f0
Compare ded3381
to 93a8e5a
Compare 5cfaf42
to 86b5710
Compare Do not merge yet while I work out the final details of this initial implementation and we get approval on the design. |
86b5710
to fe6aa6d
Compare This checks the runtime versions. Note that the defaults mean - we get the correct behavior for 3.7 and 3.8: both of these became immediately unsupported; - we get the most conservative behavior for 3.9: it becomes immediately deprecated (desired, since Python 3.9 reaches its end of life in 2025-10), and will become unsupported once it reaches its end of life (which is a conservative policy that we may or may not want to relax in a follow-up) Still todo: echo the package name in the warning message.
The warning messages now mor eclosely match what we'we're deploying in the generator for those who don't update their api-core dependency
Sometimes the version we recommend is newer than the next non-deprecated version, so we want to inform users eagerly to reduce their toil
0f07954
to 670ba62
Compare Removing |
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.
Left a lot of comments, but many of them are me learning or leaving little optional suggestions. Let me know what you think
| ||
def get_dependency_version( | ||
dependency_name: str, | ||
) -> Tuple[Optional[PackagingVersion], 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.
would a namedtuple be overkill here?
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.
Good idea. Done.
return (parse_version(version_string), version_string) | ||
| ||
except Exception: | ||
return (None, "--") |
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 magic "--" seems like it could cause trouble. Could we make the whole output optional if there's an exception, and let callers decide how to deal with an empty value? (or even just raise an exception here for the caller to handle?)
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.
If we do keep the "--", can we make it a constant?
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 semantics are conveyed by the first element of the pair, which is None. That's what users can check. If users blindly need a string, --
seems reasonable. We could use an exception, but these warnings are not meant to be blocking, so the exception would be caught and treated as this None
would be anyway. IMO, this makes the code both clear and concise, so I'd prefer to keep this unless you feel strongly the other way.
I did make it into a constant.
| ||
def warn_deprecation_for_versions_less_than( | ||
dependent_import_package: str, | ||
dependency_import_package: 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.
nit: it might be nice if these variables were more distinct. Maybe target_package
?
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.
Done.
needs `dependency_import_package`. | ||
dependency_import_package: The import name of the dependency to check. | ||
next_supported_version: The dependency_import_package version number | ||
below which a deprecation warning will be logged. |
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.
could this be minimum_supported_version? Or am I misunderstanding it?
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.
Done
""" | ||
warn_deprecation_for_versions_less_than( | ||
dependent_import_package, "google.protobuf", "4.25.8", recommended_version="6.x" | ||
) |
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.
Is the idea that we will go back and manually add dependencies here in the future?
If so, this feels a little hidden. Can we read this from a config file, or declare it as a constant at the top of the file or something?
something like:
_DependencyConstraint = namedtuple("_DependencyConstraint", ["package_name", "min_version", "recommended_version"]) DEPENDENCIES = [ _DependencyConstraint("google.protobuf", min_version="4.25.8", recommended_version="6.x") ] def check_dependency_versions(dependent_import_package: str): for package_info in DEPENDENCIES: warn_deprecation_for_versions_less_than( dependent_import_package, package_info.package_name, package_info.min_version, recommended_version=package_info.recommended_version, )
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.
I think what we have is simple and clear, though it does feel hidden just by the fact it's at the bottom of the file.
OK, I implemented the table at the top of the file.
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.
It kind of feels like dependencies should be given as an argument too? Can was assume all sdks will want to check against the same ones?
python_beta=None, | ||
python_start=datetime.date(2018, 6, 27), | ||
python_eol=datetime.date(2023, 6, 27), | ||
), |
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.
nit: it seems a little weird that the actual version identifier is held separate from the rest of the information. Maybe they should be combined?
PYTHON_VERSION_INFO: list[VersionInfo] = [ VersionInfo( "3.7", python_beta=None, python_start=datetime.date(2018, 6, 27), python_eol=datetime.date(2023, 6, 27), ), VersionInfo( "3.8", python_beta=None, python_start=datetime.date(2019, 10, 14), python_eol=datetime.date(2024, 10, 7), ), ... ] PYTHON_VERSION_MAP = {parse_version(info.version): info for info in PYTHON_VERSION_INFO}
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.
I could see it either way, but ok, sure, done.
python_beta=FAKE_FUTURE_DATE, | ||
python_start=FAKE_FUTURE_DATE, | ||
python_eol=FAKE_FUTURE_DATE, | ||
) |
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.
are the fake versions necessary? It would be great if we could refactor to return a different template or something for an unknown Python version
If we do need to use fake ones, they might work better as a constant:
version_info = UNKNOWN_FUTURE_VERSION
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.
Well, the point is that if we don't have the version info but can at least tell whether it's in the past or the future, we do the right thing. So I think these sentinel values are not a bad way to go.
I did make them into constants, and made them private.
for version, info in sorted(PYTHON_VERSION_INFO.items()): | ||
if info.python_start <= date < info.python_eol: | ||
return f"{version[0]}.{version[1]}" | ||
return "at a supported version" |
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 string doesn't seem to fit to me. "... Please upgrade to the latest Python version, or at least Python at a supported version, and then update ..."
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.
I tweaked it. WDYT?
| ||
LOWEST_TRACKED_VERSION = min(PYTHON_VERSION_INFO.keys()) | ||
FAKE_PAST_DATE = datetime.date(1970, 1, 1) | ||
FAKE_FUTURE_DATE = datetime.date(9000, 1, 1) |
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.
I'd prefer if we could avoid these fake values, but if we need to, datetime.date.max
would probably be a better fit
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.
Done
version_string = pkg_resources.get_distribution(dependency_name).version | ||
return (parse_version(version_string), version_string) | ||
| ||
except Exception: |
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.
Do we know what exceptions to expect? Or does this need to be broad?
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.
Any exception related that comes up trying to find the version. I could try to track them down, but at the end of the day I aim for the function to return without an error, but indicating if the version could not be found. So I lean towards catching broadly, but I'm open to counterarguments.
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.
Responded to you comments. I pushed back on some, so feel free free to insist.
| ||
def get_dependency_version( | ||
dependency_name: str, | ||
) -> Tuple[Optional[PackagingVersion], 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.
Good idea. Done.
return (parse_version(version_string), version_string) | ||
| ||
except Exception: | ||
return (None, "--") |
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 semantics are conveyed by the first element of the pair, which is None. That's what users can check. If users blindly need a string, --
seems reasonable. We could use an exception, but these warnings are not meant to be blocking, so the exception would be caught and treated as this None
would be anyway. IMO, this makes the code both clear and concise, so I'd prefer to keep this unless you feel strongly the other way.
I did make it into a constant.
needs `dependency_import_package`. | ||
dependency_import_package: The import name of the dependency to check. | ||
next_supported_version: The dependency_import_package version number | ||
below which a deprecation warning will be logged. |
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.
Done
( | ||
dependent_package, | ||
dependent_distribution_package, | ||
) = _get_distribution_and_import_packages(dependent_import_package) |
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.
They're the correct terms, but they are very similar. I changed the less common term (IMO), dependent
, to consumer
.
least version {next_supported_version} of | ||
{dependency_package}. | ||
""" | ||
) |
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.
I would prefer not to because the message template is defined and directly used in one place, and it's closely coupled to the variables provided by the function. I think black-boxing it inside the function, and defining it where it's used, is clearer and more compact than defining a separate constant inside or outside the function.
python_beta=None, | ||
python_start=datetime.date(2018, 6, 27), | ||
python_eol=datetime.date(2023, 6, 27), | ||
), |
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.
I could see it either way, but ok, sure, done.
PYTHON_VERSION_UNSUPPORTED = "PYTHON_VERSION_UNSUPPORTED" | ||
PYTHON_VERSION_EOL = "PYTHON_VERSION_EOL" | ||
PYTHON_VERSION_DEPRECATED = "PYTHON_VERSION_DEPRECATED" | ||
PYTHON_VERSION_SUPPORTED = "PYTHON_VERSION_SUPPORTED" |
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.
Deprecated is still supported for now. EOL will soon be unsupported.
You're right, as read naively, these are not mutually exclusive. But in this code I am using them in a mutually exclusive way, so I clarified the semantics in docstrings. WDYT?
python_beta=FAKE_FUTURE_DATE, | ||
python_start=FAKE_FUTURE_DATE, | ||
python_eol=FAKE_FUTURE_DATE, | ||
) |
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.
Well, the point is that if we don't have the version info but can at least tell whether it's in the past or the future, we do the right thing. So I think these sentinel values are not a bad way to go.
I did make them into constants, and made them private.
for version, info in sorted(PYTHON_VERSION_INFO.items()): | ||
if info.python_start <= date < info.python_eol: | ||
return f"{version[0]}.{version[1]}" | ||
return "at a supported version" |
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.
I tweaked it. WDYT?
{min_python(version_info.python_eol)}, to continue receiving updates | ||
for {package_label} past that date. | ||
""" | ||
) |
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.
No, for the same reason as the other case. They are defined and used directly only once, and the substitution strings and the text are closely related to the logic of the function. I think it reads more clearly to have them defined inline, as they are now.
This exports a function
check_python_version
that dependent packages can use to verify the run-time version on which they are running and warn users when they need to upgrade their Python version.google.api_core
also applies this function to itself.