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

[Auto Import] - Suggest equivalents from collections.abc rather than typing #3318

Closed
michaeloliverx opened this issue Sep 12, 2022 · 19 comments
Assignees
Labels
fixed in next version (main) A fix has been implemented and will appear in an upcoming version P2

Comments

@michaeloliverx
Copy link

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.

image

@bschnurr
Copy link
Member

just to confirm you prefer collecitons.abc to be suggested sooner?

@michaeloliverx
Copy link
Author

Maybe they should be suggested instead of? The typing module versions are deprecated and will probably be removed eventually.

@erictraut
Copy link
Contributor

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:

  1. Wait until Python 3.8 goes out of support (in a few years) and then stop suggesting the typing aliases of these symbols.
  2. Conditionally suggest the non-typing aliases only if on Python >=3.9.
  3. 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.

@michaeloliverx
Copy link
Author

I didn't notice that the PEP and docs wording differ.

Docs

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.

PEP 585

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.

@luabud luabud added the P2 label Sep 13, 2022
@Avasam
Copy link

Avasam commented Sep 27, 2022

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.
image

@Hugovdberg
Copy link

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.

@mehdigmira
Copy link

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

@chassing
Copy link

At least having 'from collections.abc' in the suggestions would be beneficial alone.

@benhubsch
Copy link

Still interested in this, bump @bschnurr & co.

@Igetin
Copy link

Igetin commented May 19, 2023

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.

@flying-sheep
Copy link

flying-sheep commented Jun 26, 2023

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.

@flying-sheep
Copy link

Not having the correct import in the suggestions is getting very annoying.

Is there any way to convince Pylance to do this right?

@rchiodo
Copy link
Contributor

rchiodo commented Sep 28, 2023

It's on our backlog so it should be fixed eventually.

@flying-sheep
Copy link

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?

@erictraut
Copy link
Contributor

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.

@flying-sheep
Copy link

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.

@heejaechang
Copy link
Contributor

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

@PylanceBot PylanceBot added the fixed in next version (main) A fix has been implemented and will appear in an upcoming version label Oct 31, 2023
@bschnurr
Copy link
Member

bschnurr commented Nov 3, 2023

This issue has been fixed in prerelease version 2023.11.11, which we've just released. You can find the changelog here: CHANGELOG.md

@bschnurr bschnurr closed this as completed Nov 3, 2023
@Avasam
Copy link

Avasam commented Nov 30, 2023

Using 2023.11.13 and this is not working for me, collections.abc is never suggested to me:
image

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

No branches or pull requests