Giter Site home page Giter Site logo

Comments (3)

matthiaskramm avatar matthiaskramm commented on May 22, 2024

This seems to only happen under Python 3. Under Python 2 (with backported type annotations), the inference looks correct, i.e., balance and amount are, and deposit returns, a Union[complex, float].

In general, our Python 3 compatibility is still WIP.

from pytype.

gvanrossum avatar gvanrossum commented on May 22, 2024

(CC @ddfisher :-)

from pytype.

rchen152 avatar rchen152 commented on May 22, 2024

This no longer seems to be Python 3-specific, since I now get identical results in 2 and in 3:

from typing import Any, Dict

class Account:
    __doc__ = ...  # type: str
    balance = ...  # type: Any
    def __init__(self, opening_balance: float = ...) -> None: ...
    def deposit(self, amount) -> Any: ...

class Bank:
    _accounts = ...  # type: Dict[Any, Account]
    def __init__(self, bank_owner = ...) -> None: ...
    def deposit(self, name, amount) -> Any: ...


def main() -> None: ...

The incorrect Iterable and list are gone, but balance is still Any. That's happening because we stopped trying to infer parameter types, so doing
self.balance += amount, where amount is an unknown, causes the type of balance to become unknown as well.

Passing the --protocols flag gets us more precise types:

from typing import Any, Dict, Optional, Union

class Account:
    __doc__ = ...  # type: str
    balance = ...  # type: Union[complex, float]
    def __init__(self, opening_balance: float = ...) -> None: ...
    def deposit(self, amount: complex) -> Union[complex, float]: ...

class Bank:
    _accounts = ...  # type: Dict[Any, Account]
    def __init__(self, bank_owner = ...) -> None: ...
    def deposit(self, name, amount: complex) -> Optional[Union[complex, float]]: ...


def main() -> None: ...

So I think the way to fix this would be to make --protocols the default.

from pytype.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.