question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Published types aren't complete enough for Pyright/Pylance

See original GitHub issue

It seems a bit unfair to file this as a “bug,” when really what’s going on is that the Python community is trying to figure out what a “typed” Python library looks like. In this case, what looks like perfectly reasonable code is, in another light, not explicit enough to allow tools to determine types.

(For clarity’s sake: Pylance is Microsoft’s new language-analysis extension for Python in VS Code. Pylance is closed source, but wraps the open source type engine Pyright.)

Expected Result

I have a repo at https://github.com/fluggo/pylance-test that demonstrates this issue. Pylance 2021.5.3 now knows to bypass the stub libraries when the installed library uses py.typed to declare itself typed, and so it should be able to see that this call to encode is wrong:

import jwt

# This should be an error because payload expects Dict[str, Any]
jwt.encode(payload='subsandwich')

Actual Result

Unfortunately it doesn’t, because according to Pyright, the type is unknown:

image

This is because Pyright has decided to play it safe with type inferences. The problematic code is at the bottom of api_jwt.py:

_jwt_global_obj = PyJWT()
encode = _jwt_global_obj.encode
decode_complete = _jwt_global_obj.decode_complete
decode = _jwt_global_obj.decode

These assignments look cut and dry—PyJWT is just creating a module-level alias for these functions. But it only looks that way to a programmer—putting our language lawyer hats on, these are variables, not constants, and they can be reassigned. Since that’s the case, what are the other valid values for these variables? Pyright can’t know, and can’t assume they will be constrained to the type of their first assignment.

There’s also the issue that, if an assignment like this requires any type inference at all, the results could be different from one analysis tool to the next. This is different from the situation in TypeScript, because in TypeScript, there’s only the one implementation of type inference, whereas Python doesn’t yet have a standard for inferring types.

In Pyright’s analysis, these variables need their own type annotations, and they recommend so in their Typing Guidance for Python Libraries.

The discussion on what to do here is still ongoing in microsoft/pyright#1846, but as things sit now, this is something library implementors will have to wrestle with. There’s also a bit more background in microsoft/pylance-release#1197.

Reproduction Steps

Grab my repo above and look at it with Pylance under VS Code. Apologies to anyone not using Poetry; I’m not well versed with the Python package managers.

System Information

$ python -m jwt.help
{
  "cryptography": {
    "version": "3.4.7"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.9.4"
  },
  "platform": {
    "release": "19.6.0",
    "system": "Darwin"
  },
  "pyjwt": {
    "version": "2.1.0"
  }
}

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Tinchecommented, May 26, 2022

Looks fine to me in the latest VS Code.

Screenshot 2022-05-26 at 15 05 30

The bigger problem is the annotations seem to be wrong, key isn’t just a string.

0reactions
kkirschecommented, Sep 12, 2022

Stale doesn’t mean addressed

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pylance: The best Python extension for VS Code
However, the enemy of good is better; thus, Microsoft created Pylance, a new, improved, faster, and feature-packed Python language server.
Read more >
"at" sign (@) in Python type hints (suggested by Pylance ...
I've found a similar Stackoverflow question here but it did not get any answer. python · visual-studio-code · type-hinting · pylance · pyright....
Read more >
Using Pylance to Write Better Python Inside of Visual Studio ...
A big decision a developer has to make is what tool to use to write code? Would you like an editor that understands...
Read more >
Type annotations and itk.Image[...] - ITK Discourse
mypy does not complain. I don't understand enough of typing.Literal to figure out why it fails for pylance. Is there a type annotation...
Read more >
VS Code for python - Dan MacKinlay
Using Pyright, Pylance has the ability to supercharge your Python IntelliSense experience with rich type information, helping you write better ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found