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

Partially fixes issue 8625 #8876

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

markusschmaus
Copy link
Contributor

@markusschmaus markusschmaus commented May 23, 2020

This fixes #8625 for instances when the converter is a builtin type like typle, list, or dict.

This is only a partial fix, since for user defined generic converters a similar issue still persists.

This partial fix however covers a very common and important use case.

@markusschmaus
Copy link
Contributor Author

Is there anything I can do to get this PR reviewed?

Copy link
Collaborator

@JukkaL JukkaL 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! Unfortunately, this approach doesn't work in all cases (see my comment).


# Find the constraints on the type vars given that the result must be a
# subtype of the target type.
constraints = infer_constraints(ret_type, target_type, SUBTYPE_OF)
Copy link
Collaborator

Choose a reason for hiding this comment

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

The relevant plugin hook gets called during semantic analysis, and solving constraints is unfortunately not supported during semantic analysis, since some type definitions may be incomplete. (This is pretty confusing since most simple cases work just fine, but this will fail in some edge cases.)

Basically this would need to be implemented without using any non-trivial type operations, perhaps by hard coding the behavior for a set of builtin collections.

Copy link
Contributor Author

@markusschmaus markusschmaus Jun 8, 2020

Choose a reason for hiding this comment

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

I am aware that when this gets called with a user defined converter with type variables the type definition is incomplete and doesn't contain enough information to actually solve the issue. However for builtin collections there is enough information available.

I started out by hard coding the behavior for a set of common builtin collections as you are suggesting, but soon realized that the code I was writing was identical to infer_constraints and that for builtin collections it works as long as I manually construct the list of type variables (type_var_ids = list({const.type_var for const in constraints})).

That's why this is a partial fix. But it since this works for builtin collections, it does address the most important use case. And for all other instances it fails no worse than before.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@JukkaL Would this PR be acceptable for you if I duplicated the code of infer_constraints in a separate function to avoid any code coupling? As you say, we cannot solve this issue in general because when the plugin hook gets called type definitions may be incomplete, but we can solve this for this for builtin collections, and this PR does exactly that.

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.

False positive with attrs converter
2 participants