6

I'm looking at the standard library documentation, and I see that from typing import Sequence is just calling collections.abc under the hood.

Now originally, there was a deprecation warning/error and migration from the collections package to collections.abc for some abstract classes. See here. However, now that the abstractions have settled in a new location, is it fine to use either? I see from collections.abc import [etc] in the codebase, and I wonder if it would be more practical to just import from typing when trying to do type annotations?

Cython source code: Sequence = _alias(collections.abc.Sequence, 1)

2
  • 1
    Use from collections.abc imports for everything that is there. "Importing those from typing is deprecated" Commented Mar 12, 2024 at 23:41
  • 2
    If you use ruff, you can write from typing import... and get it correctly moved to collections.abc and re by autofixer without typing that manually, it's quite convenient. Commented Mar 12, 2024 at 23:43

1 Answer 1

7

However, now that the abstractions have settled in a new location, is it fine to use either?

It's better not to do so. The documentation specifically says:

class typing.Sequence(Reversible[T_co], Collection[T_co])

Deprecated alias to collections.abc.Sequence.

Deprecated since version 3.9: collections.abc.Sequence now supports subscripting ([])

If it's deprecated, it may be removed in the next versions of Python. So go with collections.abc generic classes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.