Skip to content

Don't resolve Callable NamedTuple fields to their return type - #6576

Merged
msullivan merged 2 commits into
python:masterfrom
FuegoFro:named_tuple_callable_field
Mar 21, 2019
Merged

msullivan merged 2 commits into
python:masterfrom
FuegoFro:named_tuple_callable_field

Conversation

@FuegoFro

Copy link
Copy Markdown
Contributor

NamedTuple fields are always marked an properties. In certain situations, this causes their type to be determined incorrectly when they hold Callables, resolving to the Callable's return type rather than the Callable itself. This prevents that and fixes #6575.

`NamedTuple` fields are always marked an properties. In certain situations, this causes their type to be determined incorrectly when they hold `Callable`s, resolving to the `Callable`'s return type rather than the `Callable` itself. This prevents that and fixes python#6575.
@msullivan

Copy link
Copy Markdown
Collaborator

I am going to look at the bug you filed and think a bit about the whether this is the best approach (or whether, as you suggested, we should change something on the namedtuple side setting properties), but separate from that: could you please add a test.

@msullivan
msullivan self-requested a review March 21, 2019 00:35
Comment thread mypy/subtypes.py Outdated
assert isinstance(typ, FunctionLike)
signature = bind_self(typ, subtype)
if node.is_property:
if node.is_property and not node.info.is_named_tuple:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hm, it looks like this will still do bind_self on the type, which I think might cause trouble, since it will modify the type of the field in an unexpected way. Make sure to test with multiple arguments.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looking at https://github.com/python/mypy/blob/master/mypy/checkmember.py#L495, it seems that only is_initialized_in_class vars get the property/self binding treatment, so probably the right approach is to match that.

@FuegoFro

Copy link
Copy Markdown
Contributor Author

Thanks for the response! Yeah, I'll take a stab at adding some tests for various scenarios, including where the Callable takes multiple arguments 🙂

@FuegoFro

Copy link
Copy Markdown
Contributor Author

Thanks for the pointers here! I changed the 'self' binding check to match the code/condition you linked to 🙂 I also added some integration tests. I haven't really worked with this codebase much before, so I wasn't sure if there's a way I should try to make these work in the testsubtypes.py file. Let me know if there's a better way to add tests, or any other types of tests you'd like to see!

I did notice that a TestPEP561.test_typedpkg_editable test was failing locally, but it failed for me even on master and it seems unrelated to this change. However, if that does seem relevant and you have some pointers I can dig into it more 🙂

@msullivan msullivan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks!

Just writing end-to-end typechecker tests is totally fine and pretty standard for mypy. While we have some unit tests, the easiest way to specify programming language constructs is generally with concrete syntax so...

The TestPEP561 are frustratingly fragile. Are you testing inside a venv? TestPEP561 uses a virtualenv and nesting virtualenvs inside venvs and vice-versa tend to break in "interesting" ways.

@msullivan
msullivan merged commit a642784 into python:master Mar 21, 2019
@FuegoFro
FuegoFro deleted the named_tuple_callable_field branch March 22, 2019 00:49
@FuegoFro

Copy link
Copy Markdown
Contributor Author

Thank you for you help and quick responses/turnaround! Don't think I was in a venv, but it sounds like this wasn't entirely unexpected, so I'll hold off on looking into my local setup until/unless I have more PRs to send 😉

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.

NamedTuples can't match Protocols with Callable fields

2 participants