ty icon indicating copy to clipboard operation
ty copied to clipboard

Ty Seems Like Cannot Reveal Type From Pydantic Validation

Open scelikcapa opened this issue 2 months ago • 2 comments

Summary

I have validated the request response with a Pydantic model. And explicitly give a return type to my request method. But ty still adds "Unknown" type to the return type

I have added 3 screenshots.

  • One of them is the model
  • The second one is the request method
  • The third one is the place where I use the request method. And you can see "Unknown" type here. So I have to type check again with isinstance() unnecessarily

1- MODEL Image

2- CLIENT REQUEST METHOD Image

3- WRONG TYPE Image

Version

v2025.76.0

scelikcapa avatar Dec 28 '25 09:12 scelikcapa

out of curiosity, what's the indicated type when you hover over result after the result = ZohoEstimateGetResponse.model_validate(obj=result) within the get_estimate function

AshishT112203 avatar Dec 28 '25 23:12 AshishT112203

Can you hover over self.zoho_books_client and share what type ty reveals for it? I wonder if it's a union with Unknown (or some other type that isn't guaranteed to have a get_estimate function.

MichaReiser avatar Dec 29 '25 10:12 MichaReiser

Yes, I suspect that you need something like zoho_books_client: ZohoBooksClient (or whatever the type is actually called) on the class body of whatever class includes the get_quote method, so we know the type of self.zoho_books_client for sure.

carljm avatar Dec 29 '25 19:12 carljm

out of curiosity, what's the indicated type when you hover over result after the result = ZohoEstimateGetResponse.model_validate(obj=result) within the get_estimate function

hello @AshishT112203 ZohoEstimate|None

scelikcapa avatar Dec 30 '25 06:12 scelikcapa

Can you hover over self.zoho_books_client and share what type ty reveals for it? I wonder if it's a union with Unknown (or some other type that isn't guaranteed to have a get_estimate function. @MichaReiser @carljm

yes, it's Unknown|ZohoBooksClient. But this is weird because I see it as ZohoBooksClient only in init()

Image Image

scelikcapa avatar Dec 30 '25 06:12 scelikcapa

I have a type annotation, so this is not the same case as another issue, I think @carljm @MichaReiser

But Ty change the type and adds Unknown to my type annotation in init() when self.zoho_books_client is used in another function

scelikcapa avatar Dec 30 '25 06:12 scelikcapa

@scelikcapa can you show the actual code of your __init__ method? A type annotation on the parameter to __init__ is not (currently) enough, there would need to be one on the self.zoho_books_client = ... assignment. Or else a class-level zoho_books_client: ZohoBooksClient annotation.

carljm avatar Dec 30 '25 06:12 carljm

@carljm

class ZohoService:
    def __init__(self, zoho_crm_client: ZohoCrmClient, zoho_books_client: ZohoBooksClient):
        self.zoho_crm_client = zoho_crm_client
        self.zoho_books_client = zoho_books_client


    async def get_quote(self, quote_id: int) -> ZaqQuote:
        logger.info({"event": "fetching_zoho_quote", "quote_id": quote_id})
        zoho_estimate = await self.zoho_books_client.get_estimate(estimate_id=quote_id)
        if not isinstance(zoho_estimate, ZohoEstimate):
            raise ZohoQuoteNotFoundException(quote_id=quote_id)

        if not zoho_estimate.custom_field_hash.aq_project_id:
            raise ZohoQuoteDoNotLinkToAqProjectException(quote_id=quote_id)

        quote = Mapper.ZohoToDomain.estimate_to_zaq_quote(estimate=zoho_estimate)
        return quote

scelikcapa avatar Dec 30 '25 06:12 scelikcapa

@carljm yes, when I add to type directly to self.zoho_books_client like below, it solves the issue

class ZohoService:
    def __init__(self, zoho_crm_client: ZohoCrmClient, zoho_books_client: ZohoBooksClient):
        self.zoho_crm_client = zoho_crm_client
        self.zoho_books_client: ZohoBooksClient = zoho_books_client

scelikcapa avatar Dec 30 '25 07:12 scelikcapa

@carljm It would be better if ty reveals a type from init.

But by the way, great language server. Very appreciated.

I remove Pylance. It kills my RAM and computer, which have 16GB. Very happy to find a solution with ty

scelikcapa avatar Dec 30 '25 07:12 scelikcapa