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

Inconsistent inference inside print() #6017

Closed
syimyuzya opened this issue Dec 5, 2018 · 3 comments
Closed

Inconsistent inference inside print() #6017

syimyuzya opened this issue Dec 5, 2018 · 3 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-disallow-any The disallow-any-* family of flags

Comments

@syimyuzya
Copy link

syimyuzya commented Dec 5, 2018

Just found that mypy infers the type of sum(l) in the code below differently depending on whether it is inside a print():

#! /usr/bin/python3

# Run this with `mypy --disallow-any-expr --show-column-numbers`

from typing import List

l: List[complex] = [1.0, 2.0, 3.0-2.0j]

reveal_type(sum(l))  # builtins.complex*

s = sum(l)
reveal_type(s)  # builtins.complex*
print(s)  # typechecks!

print(sum(l))  # but this line does not typecheck!
# 15:7: error: Expression type contains "Any" (has type "Union[Any, int]")

Tested with mypy 0.641 and 0.650+dev.ad2d4ba1db9699779d5705094f991d3bb3ac112d (current master branch) on Python 3.7.1, results are same.

@ilevkivskyi
Copy link
Member

Yeah, --disallow-any-expr is known to be problematic, we have received several reports about it recently.

@ilevkivskyi ilevkivskyi added bug mypy got something wrong priority-1-normal false-positive mypy gave an error on correct code labels Dec 5, 2018
@Levitanus
Copy link

Append to the false-positive stack, please:

import typing as ty


class ChildAddress(ty.Tuple[int, int]):
    """Represents MIDI (Bus, Channel) of child Track.

    Can be compared to tuple. All (busses/channels) are equal to one.
    """

    def __new__(cls, bus: int, channel: int) -> 'ChildAddress':
        return super().__new__(cls, (bus, channel))
        # Argument 2 to "__new__" of "tuple" has incompatible type "Tuple[int, int]"; expected "Iterable[_T_co]"

    def __init__(self, bus: int, channel: int) -> None:
        """make immutable address.

        Parameters
        ----------
        bus : int
        channel : int
        """
        super().__init__()

    @property
    def bus(self) -> int:
        """MIDI Bus.

        Returns
        -------
        int
            0 if All buses
            -1 if no midi routing
        """
        return self[0]

    @property
    def channel(self) -> int:
        """Midi Channel.

        Returns
        -------
        int
            0 if All channels
            -1 if no midi routing
        """
        return self[1]

    def __eq__(self, other: object) -> bool:
        if not isinstance(other, ty.Sequence):
            return False
        if len(other) != 2:
            return False
        for item in other:
            if not isinstance(item, int):
                return False
            if item > 16:
                return False
        other = ty.cast(ty.Tuple[int, int], other)
        if other[0] != self[0]:
            for item in ty.cast(ty.Tuple[int, int], (other[0], self[0])):
                #  Redundant cast to "Tuple[int, int]"
                if item != '0':  # Expression has type "Any"
                    return False
                else:
                    break
        if other[1] != self[1]:
            for item in (other[1], self[1]):
                if item != 0:  # Expression has type "Any"
                    return False
                else:
                    break
        return True

    def __repr__(self) -> str:
        return f'ChildAddress(bus={self.bus}, channel={self.channel})'

    def __hash__(self) -> int:
        return hash((self[0], self[1]))

@AlexWaygood AlexWaygood added the topic-disallow-any The disallow-any-* family of flags label Mar 27, 2022
@AlexWaygood
Copy link
Member

The false-positive error is no longer emitted on mypy 0.700+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-disallow-any The disallow-any-* family of flags
Projects
None yet
Development

No branches or pull requests

4 participants