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

feat: prioritize specialized providers over generic #77

Merged
merged 1 commit into from
Nov 28, 2023

Conversation

jokasimr
Copy link
Contributor

Fixes #69

A type tuple a is considered more "special" than a type tuple b if the number of TypeVars in a is lower than in b.

Copy link
Member

@SimonHeybrock SimonHeybrock left a comment

Choose a reason for hiding this comment

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

Implementation looks ok, but there is a whole bunch of tests missing for generics with multiple typevars and various partial specializations.

if _is_compatible_type_tuple(requested, args)
),
)
matches = match_groups[min(match_groups.keys(), default=0)]
Copy link
Member

Choose a reason for hiding this comment

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

I found the use of defaultdict a bit confusing when reading 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.

The use of defaultdict comes from the groupby, for all intents and purposes it could have been a regular dict. In line 473 the providers with the fewest TypeVar arguments are passed on and the rest are dropped.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

whole bunch of tests missing for generics with multiple typevars and various partial specializations.

Yeah you're right. I'll fix that.

Copy link
Member

Choose a reason for hiding this comment

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

My point is: If there are no matches, then you would get a KeyError here, unless you use defaultdict, which is the wrong exception type, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah okay I see, the logic should be made clearer there, it's just a fluke that we don't get a bug because match_groups happens to be defaultdict.

@jokasimr jokasimr force-pushed the specialized-providers branch from c84ad58 to fb43e78 Compare November 28, 2023 08:25
@jokasimr jokasimr enabled auto-merge November 28, 2023 08:26
@jokasimr jokasimr merged commit 85f1251 into main Nov 28, 2023
5 checks passed
@jokasimr jokasimr deleted the specialized-providers branch November 28, 2023 08:28
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.

Allow specialized providers of generic types.
2 participants