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

Provide an accurate return type for Injected and SyncInjected #36

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

eirikur-nc
Copy link

What

Specify return type for Injected and SyncInjected

Why

Previously, the return type was Any. It was therefore necessary to specify the type of every injected argument like so

def some_route(self, foo: Foo = Injected(Foo), bar: Bar = Injected(Bar)):

For developers who rely on a static type checker that infers argument types by default values (e.g. PyRight), this can now be simplified to

def some_route(self, foo=Injected(Foo), bar=Injected(Bar)):

without loss of type information.

As far as I can tell, mypy still doesn't infer argument types like PyRight does (see python/mypy#3090). I have therefore left the code examples as-is.

As a result, it's no longer necessary to specify the type of parameters for type checkers that infer parameter types by default arguments (e.g. PyRight). Hence,
def foo(bar: Bar = Injected(Bar)):
becomes
def foo(bar=Injected(Bar)):
without loss of type information.
@matyasrichter
Copy link
Owner

Hi! Happy to merge this, thanks! Can you take a look at the failing linter? Feel free to adjust things - I just don't really have time to look into it right now.

@eirikur-nc
Copy link
Author

Shoot, we're now running into this long-standing issue with mypy and abstract base classes
python-injector/injector#143

And a whole lot more context here
python/mypy#4717

Would you be OK with disabling this particular rule as suggested here?
python/mypy#4717 (comment)

@matyasrichter
Copy link
Owner

TBH I'm fine even with switching the linter to pyright, I've had a better experience with it on other projects anyway. Disabling the rule is also okay, though. If you could do it it would be great, otherwise I'll get to it during the week.

@eirikur-nc
Copy link
Author

TBH I'm fine even with switching the linter to pyright, I've had a better experience with it on other projects anyway. Disabling the rule is also okay, though. If you could do it it would be great, otherwise I'll get to it during the week.

I took the path of least resistance and disabled the rule. But I share your sentiment. Pyright is easier to get along with.

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.

2 participants