Ty Seems Like Cannot Reveal Type From Pydantic Validation
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
2- CLIENT REQUEST METHOD
3- WRONG TYPE
Version
v2025.76.0
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
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.
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.
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
Can you hover over
self.zoho_books_clientand share what type ty reveals for it? I wonder if it's a union withUnknown(or some other type that isn't guaranteed to have aget_estimatefunction. @MichaReiser @carljm
yes, it's Unknown|ZohoBooksClient. But this is weird because I see it as ZohoBooksClient only in init()
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 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
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
@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
@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