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

Support deffered types #2

Merged
merged 9 commits into from
Jan 27, 2024

Conversation

nrbnlulu
Copy link
Contributor

@nrbnlulu nrbnlulu commented Jan 25, 2024

fix #1

@nrbnlulu nrbnlulu changed the title add failing test, Support deffered types Jan 25, 2024
Copy link
Owner

@ThirVondukr ThirVondukr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, I'd make a couple of changes, but everything looks good, I also tested it on my work codebase with around 200 dependencies and it works fine, though we didn't have to use deferred types, mostly because it'll probably mean that you'd have a circular dependency somewhere.

Comment on lines +36 to +37
if klass_name := getattr(provider_type, "__name__", None):
self.type_context[klass_name] = provider_type
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, but I think it's safe to assume that all python objects have __name__ attribute? 🤔

Suggested change
if klass_name := getattr(provider_type, "__name__", None):
self.type_context[klass_name] = provider_type
self.type_context[provider_type.__name__] = provider_type

aioinject/providers.py Outdated Show resolved Hide resolved
aioinject/providers.py Outdated Show resolved Hide resolved
@nrbnlulu
Copy link
Contributor Author

mostly because it'll probably mean that you'd have a circular dependency somewhere.

I needed that due to if TYPE_CHECKING blocks.
If have some abstract class that have dependencies you usually won;t use them in the abstract class itself.
assuming that the implementations are in different modules.

@ThirVondukr
Copy link
Owner

mostly because it'll probably mean that you'd have a circular dependency somewhere.

I needed that due to if TYPE_CHECKING blocks. If have some abstract class that have dependencies you usually won;t use them in the abstract class itself. assuming that the implementations are in different modules.

I'm not saying you're doing something wrong, you must've had a valid use case for that, also probably with new python versions when from __future__ import annotation behavior becomes the default that change would have to be made, though, not sure how that would work anyway.

impl: Any
lifetime: DependencyLifetime
_cached_dependencies: tuple[Dependency[object], ...]
_cached_type: type[_T] # TODO: I think it is redundant.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think? I think this can be removed. I can find a valid usecase for this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should provide the type in the provider I think no?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should provide the type in the provider I think no?

Not necessarily, in most cases type is derived from the object you pass into provider, e.g. Scoped(Test) would have type Test

@ThirVondukr
Copy link
Owner

I didn't include settings default = None for _cached_dependencies and _cached_type, but I think expecting AttributeError should work too, I think this can be merged

@ThirVondukr ThirVondukr merged commit 2654a4e into ThirVondukr:main Jan 27, 2024
0 of 2 checks passed
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.

Resolve type hints from the container knowledge.
2 participants