Comments (3)
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.
(CC @ddfisher :-)
from pytype.
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)
- Crash on @property + @deprecated HOT 2
- False positive for dictionary entries in version 2023.11.21 HOT 1
- `typing_extensions.Required` (PEP 655) unsupported
- Invalid annotation for torch.Tensor, cp.ndarray etc HOT 1
- Pytype does not recognize properly Sequence and Iterable collection when their size is larger than 63. HOT 1
- False-positive name-error when using `@dataclass` decorator HOT 1
- Type narrowing narrows type too much for TypeVar HOT 2
- Pytype doesn't report an error when a dict is passed with types other than those specified in the function signature HOT 1
- using Optional in TypedDict provides not consistent behaviour with PEP 589 HOT 1
- Support PEP 655 – Marking individual TypedDict items as required or potentially-missing
- Support PEP 675 – Arbitrary Literal String Type
- Support PEP 681 – Data Class Transforms
- ParseError (no attribute 'id') for bindings generated by pybind11-stubgen
- PyType permits usage of two different types for one TypeVar. HOT 1
- Support PEP 692 – Using TypedDict for more precise **kwargs typing
- PyType permits usage of two different types for one TypeVar for Sequance HOT 1
- PyType permits usage of two different types for one TypeVar for Iterable
- pytype doesn't raise an error when singleton type (enum) is used in Union HOT 1
- pytype doesn't raise any errors in operations between Union of types (int | str) and a single int literal HOT 1
- Type dict key redeclaration HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pytype.