Skip to content

use base type as hint when inferring#12284

Closed
Riolku wants to merge 4 commits into
python:masterfrom
Riolku:hint-base
Closed

use base type as hint when inferring#12284
Riolku wants to merge 4 commits into
python:masterfrom
Riolku:hint-base

Conversation

@Riolku

@Riolku Riolku commented Mar 3, 2022

Copy link
Copy Markdown

Would help with #12268. Modified a test because the two behaviours conflict right now.

Would help with python#12268. Modified a test because the two behaviours 
conflict right now.
@github-actions

This comment has been minimized.

@Riolku

Riolku commented Mar 4, 2022

Copy link
Copy Markdown
Author

To motivate this PR, consider the following code (which is also a test case):

from typing import Union, List

class A: pass
class B: pass

U = Union[A, B]

class Base:
    variable1 : List[U] = []
    variable2 : List[U] = []

class Derived(Base):
    variable1 = [A()]
    variable2 = variable1

before this PR, although variable1 is type-checked against List[U], it is inferred as List[A], which makes the last line an error. This PR instead uses the base type as a hint for the inference, if possible.

@Riolku

Riolku commented Mar 4, 2022

Copy link
Copy Markdown
Author

Note that this hinting is already done, in order to infer [A()] as List[U], so that that line doesn't throw.

@Riolku

Riolku commented Mar 4, 2022

Copy link
Copy Markdown
Author

Since the issue is open, I think it's fair to say that this would fix #12268

@Riolku

Riolku commented Mar 4, 2022

Copy link
Copy Markdown
Author

Sorry for a bit of spam, normally I'd force push, I can't force github to link the issue, for some reason.

@Riolku

Riolku commented Mar 4, 2022

Copy link
Copy Markdown
Author

I give up. Maybe a maintainer can help.

@github-actions

This comment has been minimized.

1 similar comment
@github-actions

github-actions Bot commented Mar 4, 2022

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@Riolku

Riolku commented Mar 18, 2022

Copy link
Copy Markdown
Author

Hey, I think this PR is ready for someone to look over. It's been around two weeks, and I don't want it to get lost in the sea of PRs. Does anyone mind taking a look?

@hauntsaninja hauntsaninja 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.

Thank you for the PR! I believe this was fixed in #13494

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