Closed jasonprado closed 4 months ago
@jasonprado
Please, try adding .cast
when passing service_url and my_client, like this:
class MyContainer(BaseContainer):
service_url = providers.Resource(get_service_url)
my_client = providers.Factory(MyClient, service_url=service_url.cast)
my_service = providers.Factory(MyService, client=my_client.cast)
@lesnik512 Adding .cast
seems to do the right thing, thanks!
In case this is useful to anyone else, I tried making mypy find errors in a container and I haven't been able to yet.
Correct code, no .cast
:
class MyContainer(BaseContainer):
service_url = providers.Resource(get_service_url)
my_client = providers.Factory(MyClient, service_url=service_url)
my_service = providers.Factory(MyService, client=my_client)
mypy errors: None
pyright errors: "Resource[str]" is incompatible with "str" (reportArgumentType)
as above
Correct code, with cast
:
class MyContainer(BaseContainer):
service_url = providers.Resource(get_service_url)
my_client = providers.Factory(MyClient, service_url=service_url.cast)
my_service = providers.Factory(MyService, client=my_client.cast)
mypy errors: None pyright errors: None
Incorrect code, with cast
:
class MyContainer(BaseContainer):
service_url = providers.Resource(get_service_url)
my_client = providers.Factory(MyClient, service_url=service_url.cast)
my_service = providers.Factory(MyService, client=service_url.cast) # This should be an error
mypy errors: None
pyright errors: Argument of type "Resource[str]" cannot be assigned to parameter "client" of type "MyClient"
It doesn't seem like mypy can find type errors with this kind of generic/ParamSpec
usage.
I didn't find a way to make mypy checking signatures for classes. Because looks like there is no way to describe signature of class's init function
But mypy is able to check params for functions and generators.
I'm seeing typecheck errors from pyright when I make a
Container
in what I think is an idiomatic way.My environment:
Code:
program output:
mypy output:
pyright output: