Skip to content

Conversation

vchudnov-g
Copy link
Contributor

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.

@vchudnov-g vchudnov-g requested review from ohmayr and parthea July 29, 2025 21:35
@vchudnov-g vchudnov-g requested review from a team as code owners July 29, 2025 21:35
@product-auto-label product-auto-label bot added the size: l Pull request size is large. label Jul 29, 2025
@vchudnov-g
Copy link
Contributor Author

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.

@vchudnov-g vchudnov-g marked this pull request as draft July 29, 2025 21:46
@vchudnov-g vchudnov-g force-pushed the python-version-support-check branch from 2139b90 to 4d49aa0 Compare July 30, 2025 20:39
@vchudnov-g vchudnov-g force-pushed the python-version-support-check branch 2 times, most recently from d727f14 to 17ef4f0 Compare August 12, 2025 19:23
@vchudnov-g vchudnov-g force-pushed the python-version-support-check branch from ded3381 to 93a8e5a Compare September 5, 2025 19:53
@vchudnov-g vchudnov-g marked this pull request as ready for review September 9, 2025 20:33
@vchudnov-g vchudnov-g force-pushed the python-version-support-check branch 3 times, most recently from 5cfaf42 to 86b5710 Compare September 19, 2025 17:28
@vchudnov-g vchudnov-g added the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Sep 24, 2025
@vchudnov-g
Copy link
Contributor Author

Do not merge yet while I work out the final details of this initial implementation and we get approval on the design.

@vchudnov-g vchudnov-g force-pushed the python-version-support-check branch from 86b5710 to fe6aa6d Compare October 1, 2025 18:06
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.
@vchudnov-g vchudnov-g force-pushed the python-version-support-check branch from 0f07954 to 670ba62 Compare October 10, 2025 18:17
@vchudnov-g
Copy link
Contributor Author

Removing do not merge, as the high-level policy has been approved.

@vchudnov-g vchudnov-g removed the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Oct 10, 2025
Copy link
Contributor

@daniel-sanche daniel-sanche left a 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]:
Copy link
Contributor

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?

Copy link
Contributor Author

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, "--")
Copy link
Contributor

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?)

Copy link
Contributor

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?

Copy link
Contributor Author

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,
Copy link
Contributor

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?

Copy link
Contributor Author

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.
Copy link
Contributor

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?

Copy link
Contributor Author

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"
)
Copy link
Contributor

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, ) 
Copy link
Contributor Author

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.

Copy link
Contributor

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),
),
Copy link
Contributor

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} 
Copy link
Contributor Author

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,
)
Copy link
Contributor

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 
Copy link
Contributor Author

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"
Copy link
Contributor

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 ..."

Copy link
Contributor Author

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)
Copy link
Contributor

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

Copy link
Contributor Author

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:
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

@vchudnov-g vchudnov-g left a 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]:
Copy link
Contributor Author

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, "--")
Copy link
Contributor Author

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.
Copy link
Contributor Author

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)
Copy link
Contributor Author

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}.
"""
)
Copy link
Contributor Author

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),
),
Copy link
Contributor Author

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"
Copy link
Contributor Author

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,
)
Copy link
Contributor Author

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"
Copy link
Contributor Author

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.
"""
)
Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: l Pull request size is large.

3 participants