Skip to content

Equivalent of mypy "allow_redefinition"? #2374

Answered by erictraut
ronf asked this question in Q&A
Discussion options

You must be logged in to vote

I don't think it's safe for a type checker to assume that path is narrowed to a str after the first if block. If no OSError exception is raised, path will retain its value of None.

If you are sure that your logic is sound and there's no way for path to retain its None value at the end of the first if statement, then I recommend adding an assert statement to verify this assumption. This will also tell pyright about your assumption and allow it to apply the type narrowing.

    if not path:
        try:
            transport = FooTransport()
        except OSError:
            path = _DEFAULT_PATH
        
        assert path is not None

Replies: 1 comment 6 replies

Comment options

You must be logged in to vote
6 replies
@ronf
Comment options

@erictraut
Comment options

@ronf
Comment options

@erictraut
Comment options

@ronf
Comment options

Answer selected by ronf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants