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

Allow function arguments as base classes #14135

Merged
merged 4 commits into from
Nov 21, 2022

Conversation

ilevkivskyi
Copy link
Member

@ilevkivskyi ilevkivskyi commented Nov 18, 2022

Fixes #5865

Looks quite easy and safe, unless I am missing something. Most changes in the diff are just moving stuff around.

Previously we only applied argument types before type checking, but it looks like we can totally do this in semantic analyzer. I also enable variable annotated as type (or equivalently Type[Any]), this use case was mentioned in the comments.

This PR also accidentally fixes two additional bugs, one related to type variables with values vs walrus operator, another one for type variables with values vs self types. I include test cases for those as well.

@github-actions

This comment has been minimized.

@ilevkivskyi
Copy link
Member Author

The homeassistant errors are caused by exposing an existing bug in walrus. On master this test

[case testNarrowOnSelfInGeneric]
# flags: --strict-optional
from typing import Generic, TypeVar, Optional

T = TypeVar("T", int, str)

class C(Generic[T]):
    x: Optional[T]
    def meth(self) -> Optional[T]:
        if (y := self.x) is not None:
            return reveal_type(y)
        return None

reveals type Any. But with this PR it becomes optional, so it is still wrong. I will first try to figure out what is going on.

@ilevkivskyi
Copy link
Member Author

OK, I have a fix for the walrus bug, since it is an obvious bug, I am going to just add it to this PR.

@github-actions

This comment has been minimized.

@ilevkivskyi
Copy link
Member Author

Looking again at mypy_primer it looks like this PR accidental fixes another bug related to type vars with values, this type for self types. I am going to include a regression for that one as well.

@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

artigraph (https://github.com/artigraph/artigraph)
+ tests/arti/producers/test_producer.py:679: error: Unused "type: ignore" comment
+ tests/arti/producers/test_producer.py:680: error: Unused "type: ignore" comment

Tanjun (https://github.com/FasterSpeeding/Tanjun)
- tanjun/dependencies/reloaders.py:566: error: Cannot determine type of "real_path"  [has-type]
- tanjun/dependencies/reloaders.py:567: error: Cannot determine type of "time"  [has-type]
- tanjun/dependencies/reloaders.py:578: error: Cannot determine type of "info"  [has-type]
- tanjun/dependencies/reloaders.py:579: error: Cannot determine type of "time"  [has-type]
- tanjun/dependencies/reloaders.py:599: error: Cannot determine type of "path"  [has-type]
- tanjun/dependencies/reloaders.py:600: error: Cannot determine type of "tracked_value"  [has-type]
- tanjun/dependencies/reloaders.py:609: error: Cannot determine type of "path"  [has-type]
- tanjun/dependencies/reloaders.py:609: error: Cannot determine type of "path_info"  [has-type]
- tanjun/commands/menu.py:580: error: Incompatible return value type (got "MenuCommand[_MenuCommandCallbackSigT, _MenuTypeT]", expected "Self")  [return-value]

mypy (https://github.com/python/mypy)
+ mypy/stubtest.py:357: error: Unused "type: ignore[valid-type]" comment

cloud-init (https://github.com/canonical/cloud-init)
+ cloudinit/templater.py:44: error: Unused "type: ignore" comment

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.

Nice -- fixing three bugs for the price of one!

I can't see why setting the argument types in the semantic analyzer would be a problem.

Left a comment that is mostly unrelated to this PR, feel free to ignore it if you are not sure.

mypy/types.py Show resolved Hide resolved
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.

Function argument rejected as a base class
2 participants