Git Product home page Git Product logo

Comments (6)

jacobtylerwalls avatar jacobtylerwalls commented on July 4, 2024 1

Yep. Or slightly more expressively typing.assert_never(suffix) in case it's ever reached at runtime to see what it was called with.

from pylint.

jacobtylerwalls avatar jacobtylerwalls commented on July 4, 2024 1

I think you can import it from typing_extensions, although I haven't tried it myself.

from pylint.

jacobtylerwalls avatar jacobtylerwalls commented on July 4, 2024

A potential workaround would be to explicitly mark modified will be set using some kind of asserts mechanism or comment that exists in the Typescript language (see here).

This what we've decided to support. Since pylint 3.2.1 you can use from typing import assert_never to assert exhaustiveness.

Will add your example to the docs 👍

from pylint.

mikez avatar mikez commented on July 4, 2024

@jacobtylerwalls Thanks for the fast response.

To clarify, you'd recommend a pattern like this one:

    if suffix == "d":
        modifier = f"-{number} days"
    elif suffix == "w":
        modifier = f"-{number * 7} days"
    elif suffix == "y":
        modifier = f"-{number} years"
    else:
        typing.assert_never()

from pylint.

mikez avatar mikez commented on July 4, 2024

Yep. Or slightly more expressively typing.assert_never(suffix) in case it's ever reached at runtime to see what it was called with.

Great. Thank you. 😊🙏

Feel free to close this.

from pylint.

mikez avatar mikez commented on July 4, 2024

@jacobtylerwalls Small follow-up: how do I do this in a backwards compatible way, so it also works on Python 3.8, 3.9, and 3.10? Something like this?

try:
    from typing import assert_never
except ImportError:
    # backwards compatability with Python 3.8, 3.9, 3.10
    def assert_never(arg: NoReturn, /) -> NoReturn:
        """Assert line is unreachable."""
        raise AssertionError(arg)

from pylint.

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.