Skip to content

Preserve can_be_true and can_be_false when copying types #7991

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
Nov 21, 2019

Conversation

Michael0x2a
Copy link
Collaborator

This pull request modifies the last_known_value eraser so it always copies over the previously inferred value of the can_be_true and can_be_false fields.

This isn't entirely typesafe -- for example, we can sometimes accidentally infer the wrong result if we try reassigning variables or call a mutating function :

def bad1(a: List[str], b: bool) -> bool:
    x = a and b
    a = ["foo"]
    y: bool
    return x or y   # Should be an error

def bad2(a: List[str], b: bool) -> bool:
    x = a and b
    a.append("foo")
    y: bool
    return x or y   # Should be an error

But this was apparently the old behavior/something mypy wasn't previously able to detect, so I guess this is fine for now.

I also decided against modifying every copy_modified method in all the type classes because of this reason: I wanted to limit the spread of this potentially misleading additional inferred info.

Fixes #7986, probably.

This pull request modifies the `last_known_value` eraser so it
always copies over the previously inferred value of the
`can_be_true` and `can_be_false` fields.

This isn't entirely typesafe -- for example, we can sometimes
accidentally infer the wrong result if we try reassigning variables
or call a mutating function :

    def bad1(a: List[str], b: bool) -> bool:
        x = a and b
        a = ["foo"]
        y: bool
        return x or y   # Should be an error

    def bad2(a: List[str], b: bool) -> bool:
        x = a and b
        a.append("foo")
        y: bool
        return x or y   # Should be an error

But this was apparently the old behavior/something mypy wasn't
previously able to detect, so I guess this is fine for now.

I also decided against modifying every `copy_modified` method in
all the type classes because of this reason: I wanted to limit
the spread of this potentially misleading additional inferred info.

Fixes python#7986, probably.
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!

Yes, the original behavior that this reverts to isn't type safe. Preserving the old behavior for now seems reasonable to avoid false positives. We can revisit this later and either try to fix the unsafety without introducing false positives, or decide that the false positives are acceptable.

@JukkaL JukkaL merged commit a2e010d into python:master Nov 21, 2019
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.

Unexpected conditional expression type inference change
2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy