pylance-release
pylance-release copied to clipboard
[Auto Import] - Suggest equivalents from `collections.abc` rather than `typing`
Python 3.9 allows type hinting generics in standard collections so a bunch of things from the typing namespace got deprecated. Pylance should correctly suggest the newer versions e.g. typing.Mapping > collections.abc.Mapping if the python version is >= 3.9.

just to confirm you prefer collecitons.abc to be suggested sooner?
Maybe they should be suggested instead of? The typing module versions are deprecated and will probably be removed eventually.
It's unlikely that the typing versions of these symbols will ever be removed. They are too widely used. It would be incredibly disruptive to eliminate them, and there's really no real benefit to removing them. I don't think we need to be in a hurry to move away from them.
I think there are two options here:
- Wait until Python 3.8 goes out of support (in a few years) and then stop suggesting the typing aliases of these symbols.
- Conditionally suggest the non-typing aliases only if on Python >=3.9.
- Wait until the type system is extended to include an official way to mark specific symbols as deprecated.
@michaeloliverx, I think you're suggesting option 2. The reason I'm reluctant to do this is because it involves some rather gross hard-coded logic that has special knowledge of these typing symbols. The right way to handle this in a general manner is 3. There are some discussions in the typing community for providing a way to mark symbols as deprecated, but they haven't yet gotten beyond the discussion phase.
I didn't notice that the PEP and docs wording differ.
The deprecated types will be removed from the typing module in the first Python version released 5 years after the release of Python 3.9.0. See details in PEP 585—Type Hinting Generics In Standard Collections.
The deprecated functionality may eventually be removed from the typing module. Removal will occur no sooner than Python 3.9’s end of life, scheduled for October 2025.
So I would agree that this isn't urgent.
Even if the from typing suggestion isn't removed. It would be preferable to suggest collections.abc at the top of the suggestions list (even just suggesting it at all), and auto-importing that.
What's the ETA on type system being extended to include an official way to mark specific symbols as deprecated?
Even without 3.9+ specific logic, just showing collections.abc in the Quick Fix suggestions (at the bottom) would be beneficial.

I agree that showing collections.abc before typing is preferable, exactly because of your argument it is so widely used. By showing typing as the first option this is in a way a self-fulfilling prophecy. I would hope the sort order could be updated as soon as reasonably possible, at least as soon as 3.8 is no longer supported.
If you use pyupgrade (as we do) in your CI pipelines, this is a bit annoying, since pyupgrade fails if using python 3.10 and importing these collections from typing module
At least having 'from collections.abc' in the suggestions would be beneficial alone.
Still interested in this, bump @bschnurr & co.
At least having 'from collections.abc' in the suggestions would be beneficial alone.
Lately I’ve actually noticed that sometimes it does suggest both collections.abc and typing modules for the import. But it seems pretty random, and sometimes you get just typing.
If one has Python 3.9 as minimum version, Ruff’s Pyupgrade support will be able to autofix these.
But I prefer to use them in new code, so they should be suggested.
Also, of course not everyone uses Ruff or Pyupgrade.
Not having the correct import in the suggestions is getting very annoying.
Is there any way to convince Pylance to do this right?
It's on our backlog so it should be fixed eventually.
That’s great and I appreciate it. But this is disruptive enough for me that I’d like to employ a workaround if there is one. Can you think of a way to work around this?
Out of curiosity, why are you anxious about importing from collections.abc instead of typing? The symbols are aliases for each other, so there's no runtime difference between the two. The typing symbols are unlikely to ever be removed because too much code depends on them, so your code will not stop working in the future if you import from typing. Is it just an aesthetic preference?
Can you think of a way to work around this?
Here's one workaround that admittedly has some downsides. You could uninstall pylance and install the pyright VS Code plugin. Pyright is the type checker upon which pylance is built. (You can't have pylance and pyright installed at the same time.) Pyright has the same core features of pylance but lacks advanced language server features like semantic highlighting and refactoring. It provides a simpler version of auto-import without indexing. Its auto-import suggestions are based on what it has previously seen in files that you've opened. If you open a file that imports from collections.abc, it will subsequently offer to auto-import from that location when you switch to another file. To install pyright, you'll need to first uninstall pylance and set python.languageServer to "None", then install pyright from the VS Code marketplace. It's easy to switch back if and when you find that there are pylance features that you miss.
Out of curiosity, why are you anxious about importing from collections.abc instead of typing?
I just want to use non-deprecated APIs as a baseline. And I want to use them from the start instead of changing things later. Just a good practice to have. Then your software lasts longer without changing things.
You could uninstall pylance and install the pyright VS Code plugin.
Hm, I thought something along the line of putting a file with stub overrides somewhere where PyLance picks it up or so.
For now, we will do just simple change. previously, we filtered out those types from collection.abc at deduplication pass since those are recognized as dup of ones from typing module.
for now, we will let both of them in the indices so auto/add imports show them as options as well.
but we won't touch ordering. typing will still show up before collection.abc
This issue has been fixed in prerelease version 2023.11.11, which we've just released. You can find the changelog here: CHANGELOG.md
Using 2023.11.13 and this is not working for me, collections.abc is never suggested to me: