aws / serverless-application-model

The AWS Serverless Application Model (AWS SAM) transform is a AWS CloudFormation macro that transforms SAM templates into CloudFormation templates.
https://aws.amazon.com/serverless/sam
Apache License 2.0
9.36k stars 2.38k forks source link

Several tests fail with Pydantic 1.10.15 (latest 1.x) #3600

Closed musicinmybrain closed 5 months ago

musicinmybrain commented 5 months ago

Description

Pydantic 1.10.15 aliases the pydantic namespace as pydantic.v1 as a forward-compatibility feature. This seems to interact badly with several

Steps to reproduce

$ gh repo clone aws/serverless-application-model
$ cd serverless-application-model
$ python3 -m venv _e
$ . _e/bin/activate
(_e) $ pip install -e .[dev]
(_e) $ pip install pydantic==1.10.15
(_e) $ make test

Observed result

=========================================================================================== short test summary info ============================================================================================
FAILED tests/model/test_resource_validator.py::TestResourceValidatorFailure::test_connector_with_empty_properties - AttributeError: module 'pydantic.v1' has no attribute 'error_wrappers'
FAILED tests/model/test_resource_validator.py::TestResourceValidatorFailure::test_connector_with_invalid_permission - AttributeError: module 'pydantic.v1' has no attribute 'error_wrappers'
FAILED tests/model/test_resource_validator.py::TestResourceValidatorFailure::test_connector_with_invalid_permission_type - AttributeError: module 'pydantic.v1' has no attribute 'error_wrappers'
FAILED tests/model/test_resource_validator.py::TestResourceValidatorFailure::test_connector_without_source - AttributeError: module 'pydantic.v1' has no attribute 'error_wrappers'
FAILED tests/translator/test_translator.py::test_transform_invalid_document[error_graphqlapi] - AttributeError: module 'pydantic.v1' has no attribute 'error_wrappers'
============================================================================= 5 failed, 3967 passed, 5 rerun in 104.48s (0:01:44) =============================================================================

Expected result

(_e) $ pip install pydantic==1.10.14
(_e) $ make test
======================================================================================= 3972 passed in 103.63s (0:01:43) =======================================================================================

Additional environment details

  1. OS: Fedora 39 x86_64
  2. If using the SAM CLI, sam --version: N/A
  3. AWS region: N/A
musicinmybrain commented 5 months ago

In Pydantic 1.10.14, from pydantic import v1 as pydantic raises an ImportError in

https://github.com/aws/serverless-application-model/blob/44562dc000b991b99c80798e4a35ff9fbe8397d9/samtranslator/compat.py#L1-L6

and import pydantic is used.

In Pydantic 1.10.14, from pydantic import v1 as pydantic succeeds, but pydantic.v1.error_wrappers is not present, only pydantic.error_wrappers; therefore, samtranslator.compat.pydantic.error_wrappers is not present.

Related: https://github.com/pydantic/pydantic/blob/5476a758c8ac59887dbfa3aa1c3481d0a0e20837/pydantic/v1.py#L5-L6

musicinmybrain commented 5 months ago

I’ll try to offer a PR for this later today.

musicinmybrain commented 5 months ago

I’ll try to offer a PR for this later today.

I am actually not sure what the correct approach is here. Using pydantic.ValidationError in the except …: isn’t right (on Pydantic v2, it is pydantic_core._pydantic_core.ValidationError, which has no direct inheritance relationship with pydantic.v1.error_wrappers.ValidationError).

It seems like it might be a bug that pydantic.v1.error_wrappers isn’t present in Pydantic 1.10.15, and that this might be the same as https://github.com/pydantic/pydantic/issues/9357, but I don’t have enough Pydantic experience to state that confidently.

shikha372 commented 5 months ago

Was able to reproduce this issue on local

Issue opened with pydantic team for import error https://github.com/pydantic/pydantic/issues/9625

shikha372 commented 5 months ago

Thanks @musicinmybrain for highlighting this issue. We've opened a bug at pydantic end to check this, meanwhile added this to list of expected exception at SAM end in PR#3611 . Let us know if you still have any questions regarding this.

github-actions[bot] commented 5 months ago

This issue is now closed. Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one.