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

After being changed to another string the first element of a list[str], pylance infers it as None #992

Closed
PatrickBourdon opened this issue Feb 25, 2021 · 3 comments
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@PatrickBourdon
Copy link

Environment data

  • Windows-10-10.0.18362-SP0
  • python(3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55) [MSC v.1928 64 bit (AMD64)])
  • Visual Code Studio: 1.53.2
  • extension pylance: v2021.2.4
  • type checking mode: strict

Foreword

I have just finished to (strictly) type several thousands of python lines of code using pylance.
It's a great tool that enforces to make the code more clearer and maintainable.
I encountered some issues, some were bugs, some borderline cases.
I want to thank Erik and Jake for the reactivity and for the very clear answers.
This is probably the last issue, a bit surprising.

Expected behaviour

No problem reported.

Actual behaviour

A problem reported. See example below

Code Snippet / Additional information

y: list[str] = [ 'a', 'b', 'c' ]
y[0] = 'b'
y0 = y[0]
if y[0].lower() == 'a':
    ...
# Cannot access member "lower" for type "None"
#  Type "None" is unsupported

z = y
z0 = z[0]
if z[0].lower() == 'a':
    ...
# all is correct here
@erictraut
Copy link
Contributor

Thanks for the bug report. At first glance, this looks like a bug in a feature I added in the latest release of Pylance: type narrowing for subscript (index) expressions. The type of the expression y[0] is apparently being narrowed to None when it shouldn't be. I'll need to investigate and debug.

@erictraut
Copy link
Contributor

Yes, this was a bug in the assignment-based type narrowing for index expressions. This code was recently added (appeared in the last release of pylance), and it contained a bug. My unit test explicitly assigned None, so it didn't catch the problem. I've fixed the issue and beefed up the unit test.

@PatrickBourdon, it's gratifying to hear that you're finding pylance useful. I'm impressed that you got several thousand lines of code to pass strict type checking. That's not easy to do. My team has about 250K lines of Python code, and it took us about a year and a half to get it to pass under strict type checks. Now it's much easier to maintain.

@erictraut erictraut added bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version and removed triage labels Feb 26, 2021
@jakebailey
Copy link
Member

This issue has been fixed in version 2021.3.0, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202130-3-march-2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

3 participants