Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Never" is not iterable #6415

Closed
GammaGames opened this issue Sep 19, 2024 · 2 comments
Closed

"Never" is not iterable #6415

GammaGames opened this issue Sep 19, 2024 · 2 comments
Assignees

Comments

@GammaGames
Copy link

I'm writing a fixture for pytest that returns an object with an optional kwarg. It's reporting a problem even though I've checked if the variable isn't None and made sure it's a list.

Environment data

  • Pylance version: v2024.9.1
  • OS and version: Ubuntu 22.04 LTS
  • Python version (& distribution if applicable, e.g. Anaconda): 3.10.12

Code Snippet

def fixture():
    _arg = None
    class FixtureClass:
        def do_thing(self, arg=None):
            nonlocal _arg
            if arg is not None:
                _arg = arg

    _class = FixtureClass()
    if _arg is not None:
        if isinstance(_arg, list):
            for s in _arg:
                _class.do_thing(s)
        else:
            _class.do_thing(_arg)

Repro Steps

  1. Create a new python file and paste the code in

Expected behavior

No problems

Actual behavior

image

@github-actions github-actions bot added the needs repro Issue has not been reproduced yet label Sep 19, 2024
@debonte
Copy link
Contributor

debonte commented Sep 20, 2024

Pylance is behaving properly here. _arg is always None within fixture(). Therefore, _arg is not None will always be false. That's why _arg is narrowed to Never within that if block -- there's no type that _arg could possibly have in that context.

@debonte debonte closed this as completed Sep 20, 2024
@debonte debonte added by design and removed needs repro Issue has not been reproduced yet labels Sep 20, 2024
@GammaGames
Copy link
Author

Makes sense, thank you! I should declare it with _arg : str | list | None = None instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants