|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +"""Pydantic compatibility check for sagemaker-core. |
| 14 | +
|
| 15 | +This module provides an early check for pydantic/pydantic-core version |
| 16 | +compatibility to give users a clear error message with fix instructions |
| 17 | +instead of a cryptic SystemError. |
| 18 | +""" |
| 19 | + |
| 20 | + |
| 21 | +def check_pydantic_compatibility(): |
| 22 | + """Check that pydantic and pydantic-core versions are compatible. |
| 23 | +
|
| 24 | + Raises: |
| 25 | + ImportError: If pydantic and pydantic-core versions are incompatible, |
| 26 | + with instructions on how to fix the issue. |
| 27 | + """ |
| 28 | + try: |
| 29 | + import pydantic # noqa: F401 |
| 30 | + except SystemError as e: |
| 31 | + error_message = str(e) |
| 32 | + raise ImportError( |
| 33 | + f"Pydantic version incompatibility detected: {error_message}\n\n" |
| 34 | + "This typically happens when pydantic-core is upgraded independently " |
| 35 | + "of pydantic, causing a version mismatch.\n\n" |
| 36 | + "To fix this, run:\n" |
| 37 | + " pip install pydantic pydantic-core --force-reinstall\n\n" |
| 38 | + "This will ensure both packages are installed at compatible versions." |
| 39 | + ) from e |
| 40 | + |
| 41 | + try: |
| 42 | + import pydantic_core # noqa: F401 |
| 43 | + except ImportError: |
| 44 | + # pydantic_core not installed separately is fine; |
| 45 | + # pydantic manages it as a dependency |
| 46 | + return |
| 47 | + |
| 48 | + # Additional version check: pydantic declares the exact pydantic-core |
| 49 | + # version it requires. Verify they match. |
| 50 | + try: |
| 51 | + pydantic_version = pydantic.VERSION |
| 52 | + pydantic_core_version = pydantic_core.VERSION |
| 53 | + |
| 54 | + # pydantic >= 2.x stores the required core version |
| 55 | + expected_core_version = getattr(pydantic, '__pydantic_core_version__', None) |
| 56 | + if expected_core_version is None: |
| 57 | + # Try alternative attribute name used in some pydantic versions |
| 58 | + expected_core_version = getattr( |
| 59 | + pydantic, '_internal', None |
| 60 | + ) and getattr( |
| 61 | + getattr(pydantic, '_internal', None), |
| 62 | + '_generate_schema', |
| 63 | + None, |
| 64 | + ) |
| 65 | + # If we can't determine the expected version, skip the check |
| 66 | + return |
| 67 | + |
| 68 | + if pydantic_core_version != expected_core_version: |
| 69 | + raise ImportError( |
| 70 | + f"Pydantic/pydantic-core version mismatch detected: " |
| 71 | + f"pydantic {pydantic_version} requires pydantic-core=={expected_core_version}, " |
| 72 | + f"but pydantic-core {pydantic_core_version} is installed.\n\n" |
| 73 | + "To fix this, run:\n" |
| 74 | + " pip install pydantic pydantic-core --force-reinstall\n\n" |
| 75 | + "This will ensure both packages are installed at compatible versions." |
| 76 | + ) |
| 77 | + except (AttributeError, TypeError): |
| 78 | + # If we can't determine versions, skip the check |
| 79 | + # The SystemError catch above will handle the most common case |
| 80 | + pass |
0 commit comments