Skip to content

Strange case with deepcopy() and Literal[True] #12001

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

Closed
jolaf opened this issue Jan 17, 2022 · 1 comment · Fixed by #19249
Closed

Strange case with deepcopy() and Literal[True] #12001

jolaf opened this issue Jan 17, 2022 · 1 comment · Fixed by #19249
Labels
bug mypy got something wrong topic-literal-types topic-type-context Type context / bidirectional inference

Comments

@jolaf
Copy link
Contributor

jolaf commented Jan 17, 2022

The following code:

from copy import deepcopy
from typing import Literal, Union

def f(a: str, b: bool) -> Union[Literal[True], str]:
    return b or deepcopy(a)

print(f("abc", False))
print(f("abc", True))

produces the following output:

$ python3 test.py
abc
True

$ mypy test.py
test.py: note: In function "f":
test.py:5:12: error: Incompatible return value type (got "bool", expected "Union[Literal[True], str]")  [return-value]
        return b or deepcopy(a)
               ^
test.py:5:26: error: Argument 1 to "deepcopy" has incompatible type "str"; expected "bool"  [arg-type]
        return b or deepcopy(a)
                             ^
Found 2 errors in 1 file (checked 1 source file)

$ python3 --version
Python 3.8.10

$ mypy --version
mypy 0.931

$ cat /etc/issue
Ubuntu 20.04.3 LTS \n \l

$ uname -a
Linux ubuntu 5.4.0-94-generic #106-Ubuntu SMP Thu Jan 6 23:58:14 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

If deepcopy() call is removed, warnings go away.

In some similar cases only the second warning appears, which looks even weirder.

@jolaf jolaf added the bug mypy got something wrong label Jan 17, 2022
@jolaf jolaf changed the title Strange case with deepcopy() Strange case with deepcopy() and Literal[True] Jan 17, 2022
@brianschubert brianschubert added the topic-type-context Type context / bidirectional inference label Dec 8, 2024
@brianschubert
Copy link
Collaborator

Related / duplicate of #10696, just using or instead of and. Both issues have to do with bidirectional inference going awry when analyzing the right operand using the type of the left operand as the type context.

sterliakov added a commit to sterliakov/mypy that referenced this issue Jun 11, 2025
sterliakov added a commit to sterliakov/mypy that referenced this issue Jun 11, 2025
ilevkivskyi pushed a commit to ilevkivskyi/mypy that referenced this issue Jun 13, 2025
…f binary ops (python#19249)

Fixes python#12001. Fixes python#6898. Fixes python#15368. Improves python#17790 and python#11508.

When encountering `a {and,or} b`, we used to use `a` as primary context
for `b` inference. This results in weird errors when `a` and `b` are
completely unrelated, and in many cases return type/assignment type
context can do much better. Inferring to union should be harmless in
most cases, so use union of `a` and current context instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-literal-types topic-type-context Type context / bidirectional inference
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants