Skip to content

Use collections.abc instead of deprecated typing for iterables objects#7329

Merged
geooo109 merged 17 commits intotobymao:mainfrom
OutSquareCapital:typing-improvements
Mar 20, 2026
Merged

Use collections.abc instead of deprecated typing for iterables objects#7329
geooo109 merged 17 commits intotobymao:mainfrom
OutSquareCapital:typing-improvements

Conversation

@OutSquareCapital
Copy link
Contributor

@OutSquareCapital OutSquareCapital commented Mar 18, 2026

typing.{Set, Dict, List, Tuple, Iterator, Iterable, etc...} is deprecated in 3.9.
No change wathsoever outside of pure typing constructs, so no runtime impact at all.

I originally wanted to do a full modernization with list/dict/set/tuple instead of their typing counterparts, but it was becoming way too big of a change.
Instead I changed some of them who were around my collections.abc changes.
EDIT:
sorry for the numerous workflow reruns. I hadn't understood immediatly that it would reject unformatted code. I tought that it would format it as part of the workflow.

- replaced all deprecated typing.{Iterable, Iterator, etc...} uses to correspond collections.abc types
- import builtins as typ to avoid shadowing of builtins `type` by `Expr.type` property
@geooo109
Copy link
Collaborator

@OutSquareCapital thanks for the PR.

What was the motivation behind this ? it was just to remove t. ?

There are places that we can't avoid importing typing as t (beside the containers), and we result into importing 2 type of sources for similar things.

@OutSquareCapital
Copy link
Contributor Author

It's a first step for further typing contributions. I have 2 other branches following (1 ready already).
I use basedpyright with ALL enabled, the less noise I have when checking errors (+30K currently in this codebase) the better I work.

- replaced all deprecated typing.{Iterable, Iterator, etc...} uses to correspond collections.abc types
- import builtins as typ to avoid shadowing of builtins `type` by `Expr.type` property
@geooo109 geooo109 requested a review from VaggelisD March 19, 2026 09:56
return self

def error_messages(self, args: t.Optional[t.Sequence] = None) -> t.List[str]:
def error_messages(self, args: t.Optional[Sequence[object]] = None) -> list[str]:
Copy link
Collaborator

@geooo109 geooo109 Mar 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this is an object ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnanotated generics objects wil default to typing.Any.

It won't be an error at the caller site, but will be at the definition site for stricter type checkers (pylance, basedpyright for example).

object is always preferrable to typing.Any for various reasons:

  • same "this can be anything" permissivity for most cases
  • once you start trying to access some specific attributes on a variable x, e.g len(x), x being typed as object will flag this as an error.
    This can give the opportunity to narrow the type, which is always preferable than keeping it as Any.
  • no import (no pertinent here)

Any is almost never needed.
You can always use type Union, generics or Protocols instead.
But since this PR was already pretty big I keeped it simple and just defaulted to "if unnanotated, use object if it works, else typing.Any"

Copy link
Collaborator

@geooo109 geooo109 Mar 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@OutSquareCapital I see ok, let's use the any logic for now and match the previous approach because we do a lot of changes at once. (so keep the previous approach but use Sequence instead of t.Sequence as we do for the rest of the PR). In a follow work we can focus on these cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

return walk_in_scope(self.expression, prune=prune)

def find(self, *expression_types: t.Type[E]) -> t.Optional[E]:
def find(self, *expression_types: type[E]) -> t.Optional[E]:
Copy link
Collaborator

@geooo109 geooo109 Mar 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For these cases can we import also the from builtins import type as Type and use it ? in order to be robust for all the codebase. So, everywhere we should have Type instead of t.Type or type

Copy link
Contributor Author

@OutSquareCapital OutSquareCapital Mar 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everywhere EVERYWHERE or everywhere in my current changes? The former will be easier to do but will up by a lot the nb of files impacted

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@OutSquareCapital only in the current files you changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!
I changed a few more that still used t.Type (in the same already impacted files OFC).

@geooo109
Copy link
Collaborator

@OutSquareCapital most of the changes LGTM, left some comments.

@geooo109
Copy link
Collaborator

@OutSquareCapital when you resolve both my comments, we are ready to merge.

@geooo109 geooo109 merged commit f7bb6be into tobymao:main Mar 20, 2026
8 checks passed
@OutSquareCapital OutSquareCapital deleted the typing-improvements branch March 20, 2026 12:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants