Skip to content
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

fix: remove hardcoded version in pydantic>=2 tests #3255

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

natsukium
Copy link

Description

When I run the test with pydantic>=2 (pydantic!=2.0.3), two tests fail due to the hardcoded version.
This problem has not been found because no tests are currently run against pydantic2.

Error Log
strawberry-graphql> ________________________ test_mutation_with_validation _________________________
strawberry-graphql> 
strawberry-graphql>     def test_mutation_with_validation():
strawberry-graphql>         class User(pydantic.BaseModel):
strawberry-graphql>             name: pydantic.constr(min_length=2)
strawberry-graphql>     
strawberry-graphql>         @strawberry.experimental.pydantic.input(User)
strawberry-graphql>         class CreateUserInput:
strawberry-graphql>             name: strawberry.auto
strawberry-graphql>     
strawberry-graphql>         @strawberry.experimental.pydantic.type(User)
strawberry-graphql>         class UserType:
strawberry-graphql>             name: strawberry.auto
strawberry-graphql>     
strawberry-graphql>         @strawberry.type
strawberry-graphql>         class Query:
strawberry-graphql>             h: str
strawberry-graphql>     
strawberry-graphql>         @strawberry.type
strawberry-graphql>         class Mutation:
strawberry-graphql>             @strawberry.mutation
strawberry-graphql>             def create_user(self, input: CreateUserInput) -> UserType:
strawberry-graphql>                 data = input.to_pydantic()
strawberry-graphql>     
strawberry-graphql>                 return UserType(name=data.name)
strawberry-graphql>     
strawberry-graphql>         schema = strawberry.Schema(query=Query, mutation=Mutation)
strawberry-graphql>     
strawberry-graphql>         query = """
strawberry-graphql>             mutation {
strawberry-graphql>                 createUser(input: { name: "P" }) {
strawberry-graphql>                     name
strawberry-graphql>                 }
strawberry-graphql>             }
strawberry-graphql>         """
strawberry-graphql>     
strawberry-graphql>         result = schema.execute_sync(query)
strawberry-graphql>     
strawberry-graphql>         if IS_PYDANTIC_V2:
strawberry-graphql> >           assert result.errors[0].message == (
strawberry-graphql>                 "1 validation error for User\n"
strawberry-graphql>                 "name\n"
strawberry-graphql>                 "  String should have at least 2 characters [type=string_too_short, "
strawberry-graphql>                 "input_value='P', input_type=str]\n"
strawberry-graphql>                 "    For further information visit "
strawberry-graphql>                 "https://errors.pydantic.dev/2.0.3/v/string_too_short"
strawberry-graphql>             )
strawberry-graphql> E           assert "1 validation error for User\nname\n  String should have at least 2 characters [type=string_too_short, input_value='P', input_type=str]\n    For further information visit https://errors.pydantic.dev/2.3/v/string_too_short" == "1 validation error for User\nname\n  String should have at least 2 characters [type=string_too_short, input_value='P', input_type=str]\n    For further information visit https://errors.pydantic.dev/2.0.3/v/string_too_short"
strawberry-graphql> E               1 validation error for User
strawberry-graphql> E               name
strawberry-graphql> E                 String should have at least 2 characters [type=string_too_short, input_value='P', input_type=str]
strawberry-graphql> E             -     For further information visit https://errors.pydantic.dev/2.0.3/v/string_too_short
strawberry-graphql> E             ?                                                                 --
strawberry-graphql> E             +     For further information visit https://errors.pydantic.dev/2.3/v/string_too_short
strawberry-graphql> 
strawberry-graphql> tests/experimental/pydantic/schema/test_mutation.py:84: AssertionError

Types of Changes

  • Core
  • Bugfix
  • New feature
  • Enhancement/optimization
  • Documentation

Issues Fixed or Closed by This PR

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have tested the changes and verified that they work and don't break anything (as well as I can manage).

I don't think RELEASE.md is necessary since this is a fix for internal test.

Copy link
Member

@patrick91 patrick91 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch! Thank you so much 😊

Copy link

codspeed-hq bot commented Nov 25, 2023

CodSpeed Performance Report

Merging #3255 will not alter performance

Comparing natsukium:fix/test-with-pydantic2 (0a0dc28) with main (a90dea2)

Summary

✅ 11 untouched benchmarks

Copy link

codecov bot commented Nov 25, 2023

Codecov Report

Merging #3255 (0a0dc28) into main (a90dea2) will increase coverage by 0.24%.
The diff coverage is n/a.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3255      +/-   ##
==========================================
+ Coverage   96.36%   96.60%   +0.24%     
==========================================
  Files         482      482              
  Lines       29952    29954       +2     
  Branches     3690     3694       +4     
==========================================
+ Hits        28863    28937      +74     
+ Misses        898      833      -65     
+ Partials      191      184       -7     

@patrick91
Copy link
Member

Ah looks like pydantic.version.version_short doesn't exist in pydantic 2

E AttributeError: module 'pydantic.version' has no attribute 'version_short'

@natsukium
Copy link
Author

Sorry, version_short() seems to have been introduced in 2.2.0. pydantic/pydantic#7115

I don't think it is a good idea to do if pydantic.__version__ >= 2.2.0: ... else: ..., but how about changing the test to a partial match instead of a full match?
for example,

- assert result.errors[0].message == (
+ assert (
      "1 validation error for User\n"
      "name\n"
      "  String should have at least 2 characters [type=string_too_short, "
      "input_value='P', input_type=str]\n"
      "    For further information visit "
-     f"https://errors.pydantic.dev/{pydantic.version.version_short()}/v/string_too_short"
- )
+ ) in result.errors[0].message

@patrick91
Copy link
Member

@natsukium partial match is totally fine with me 😊 but since version_short seems to working in v1 maybe we can force pydantic to be at least 2.2 in the tests? what do you think?

@natsukium
Copy link
Author

That's great.
I did a quick check and it seems that pydantic>=2.2 is fine. Do I edit the noxfile with pydantic~=2.2.0?

@patrick91
Copy link
Member

patrick91 commented Nov 26, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants