Skip to content

Allow any more general type for override of a method #1524

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

Merged
merged 1 commit into from
May 13, 2016

Conversation

rwbarton
Copy link
Contributor

This is a prerequisite for fixing #1261 because overrides with generic
function type variables currently work by accident: the type variables
have the same ids in the original method and the overriding method, so
is_subtype on the argument type returns True.

This also allows an overriding method to generalize a specific type in
the original method to a type variable, as demonstrated in one of the
tests (testOverrideGenericMethodInNonGenericClassGeneralize).

is_subtype already determines correctly whether the override is valid,
so all the hard work here is in providing a more specific error
message when possible.

This is a prerequisite for fixing python#1261 because overrides with generic
function type variables currently work by accident: the type variables
have the same ids in the original method and the overriding method, so
is_subtype on the argument type returns True.

This also allows an overriding method to generalize a specific type in
the original method to a type variable, as demonstrated in one of the
tests (testOverrideGenericMethodInNonGenericClassGeneralize).

is_subtype already determines correctly whether the override is valid,
so all the hard work here is in providing a more specific error
message when possible.
if (isinstance(override, CallableType) and
isinstance(original, CallableType) and
len(override.arg_types) == len(original.arg_types) and
override.min_args == original.min_args):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Here it would be better to have an argument kind check instead of just min_args (the latter is from early, pre-mypy days).

@JukkaL JukkaL merged commit 5a2f5c6 into python:master May 13, 2016
@JukkaL
Copy link
Collaborator

JukkaL commented May 13, 2016

No need to do anything about the above comment now, since the same issue is also present elsewhere in the code. Maybe create an issue about not using argument counts in general, instead preferring to check argument kinds. (Or I can do it.)

gvanrossum pushed a commit that referenced this pull request May 28, 2016
This is a prerequisite for fixing #1261 because overrides with generic
function type variables currently work by accident: the type variables
have the same ids in the original method and the overriding method, so
is_subtype on the argument type returns True.

This also allows an overriding method to generalize a specific type in
the original method to a type variable, as demonstrated in one of the
tests (testOverrideGenericMethodInNonGenericClassGeneralize).

is_subtype already determines correctly whether the override is valid,
so all the hard work here is in providing a more specific error
message when possible.
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