Git Product home page Git Product logo

Comments (5)

udifuchs avatar udifuchs commented on May 31, 2024 2

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.

ikonst avatar ikonst commented on May 31, 2024 1

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.

erictraut avatar erictraut commented on May 31, 2024

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.

Viicos avatar Viicos commented on May 31, 2024

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.

tbenhamou avatar tbenhamou commented on May 31, 2024

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)

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.