Comments (5)
The following code is easier to understand for both mypy and humans:
if my_options is None:
return MyClass(options=None)
else:
assert isinstance(my_options.options, CustomOptions)
return MyClass(options=my_options.options)
from mypy.
You're expecting mypy to track relationships between variables (my_options
and my_options.options
in your case), which it does not.
You can see another example discussed here:
https://mypy.readthedocs.io/en/stable/type_narrowing.html#limitations
p.s. @AlexWaygood maybe we need topic-entanglement :)
from mypy.
Could you provide a minimal, self-contained example? Your code sample references symbols that are not defined, which makes it difficult to diagnose what's happening here.
from mypy.
Here is a MRE, deduced from the example: https://mypy-play.net/?mypy=latest&python=3.12&gist=d7be1b77e92d1b2128b2b3492dfa3321
But you might be expecting to much from a type checker, maybe by simplifying a bit the logic you might get what you're trying to achieve
from mypy.
Thanks @Viicos , the MRE you provided perfectly describes my case. @erictraut FYI.
@Viicos what do you recommend to simplify the logic? I aimed to rely on the assert for its by-design runtime check instead of using a cast()
.
from mypy.
Related Issues (20)
- overloaded class-decorator is seen as not Callable when used on an overloaded method HOT 2
- 1.9.0 Release Tracking Issue HOT 10
- mypyc: `KeyError 'expr'` with NamedTuple HOT 1
- Optional[TypeVar] does not preserve type of TypeVar when given Union type HOT 1
- Overloaded function signature will never be matched
- false positive on interaction between `@property`, `@lru_cache`, and inheritance HOT 2
- False negative with property and decorator
- Crash Report: Internal Error for typechecking openai with pydantic-plugin HOT 5
- The attrs.field converter parameter does not support higher-order functions.
- Descriptors misbehave with `NoReturn`/`Never`, with `@overload`s, etc. HOT 4
- Stubgen crashes on TypeVarTuple usage
- If plugin has same name as top-level module which has been imported before mypy runs (e.g., you are using api), mypy picks up the wrong file HOT 1
- str counts as a Callable[..., str], but not as a Callable[..., TypeVar] HOT 1
- `Callable` not deduced correctly
- False positive typing.Self has incompatible type when method uses class' generic type HOT 3
- Calls to `.get` on TypeVars bound to TypedDict narrow to `object` HOT 2
- Presence of property in Protocol causes false positive in multiple inheritance scenario HOT 1
- Generate an error if using a type from the numbers module HOT 1
- Using numpy types conditionally gives confusing `[assignment]` error HOT 2
- false "Attempted to reuse member name in Enum definition"
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 mypy.