Skip to content

Conversation

zakstucke
Copy link
Contributor

@zakstucke zakstucke commented Jul 12, 2023

Change Summary

Add __init__ TYPE_CHECKING block to TypeAdaptor similar to one for __new__.

Related issue number

fix #6615

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

Selected Reviewer: @samuelcolvin

@hramezani
Copy link
Member

please review

@adriangb
Copy link
Member

I used the following little script to test:

from pydantic import TypeAdapter
from typing_extensions import reveal_type

reveal_type(TypeAdapter(int))
reveal_type(TypeAdapter(int | str))

Then mypy test.py:

test.py:4: note: Revealed type is "pydantic.type_adapter.TypeAdapter[builtins.int]"
test.py:5: note: Revealed type is "pydantic.type_adapter.TypeAdapter[types.UnionType]"

Or pyright test.py:

test.py:4:13 - information: Type of "TypeAdapter(int)" is "TypeAdapter[int]"
test.py:5:13 - information: Type of "TypeAdapter(int | str)" is "TypeAdapter[int | str]"

So this seems fine to me! Not sure why I went with __new__ originally, there was a reason, but this works so 🤷🏻‍♂️

@adriangb adriangb merged commit bddc5ba into pydantic:main Jul 12, 2023
@zakstucke zakstucke deleted the fix-ta-mypy-T-inferrence branch July 12, 2023 13:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TypeAdapter[T] inferrence fails with mypy (quick fix)
4 participants