Skip to content

Fix type annotations (from Samsung, AI Center, Cambridge)#2911

Merged
pplantinga merged 3 commits intospeechbrain:developfrom
rogiervd:fix_type_annotations
May 19, 2025
Merged

Fix type annotations (from Samsung, AI Center, Cambridge)#2911
pplantinga merged 3 commits intospeechbrain:developfrom
rogiervd:fix_type_annotations

Conversation

@rogiervd
Copy link
Contributor

@rogiervd rogiervd commented May 14, 2025

What does this PR do?

Fix existing type annotations. These are meant to be uncontroversial.

There are a few types of categories:

  • Ungrammatical or mis-spelled type annotations.
  • Arguments argument: int = None where the type should be Optional[int]
  • The opposite: arguments argument: Optional[int] = 3072 where the type should be int, because Optional means "may be None".
  • Various other cases.

These were flagged by Pyright (see #2901).

Before submitting
  • Did you read the contributor guideline?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you list all the breaking changes introduced by this pull request?
  • Does your code adhere to project-specific code style and conventions?

PR review

Reviewer checklist
  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified
  • Confirm that the changes adhere to compatibility requirements (e.g., Python version, platform)
  • Review the self-review checklist to ensure the code is ready for review

@rogiervd rogiervd changed the title Fix type annotations Fix type annotations (from Samsung, AI Center, Cambridge) May 14, 2025

@functools.lru_cache(None)
def warning_once(self, *args: tuple, **kwargs: dict):
def warning_once(self, *args, **kwargs):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

From https://typing.python.org/en/latest/spec/callables.html#annotating-args-and-kwargs:

Therefore, the definition:

def func(*args: str, **kwargs: int): ...

means that the function accepts an arbitrary number of positional arguments of type str and an arbitrary number of keyword arguments of type int.

Copy link
Collaborator

@pplantinga pplantinga left a comment

Choose a reason for hiding this comment

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

Overall, looks good. Just a few questions here

from collections import Counter
from operator import itemgetter
from typing import List
from typing import List, Optional, Union
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should List be removed here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are existing uses of List which I have not changed, but we could/should do this in another PR, now that python>=3.9.

d_ffn=2048,
dropout=0.1,
activation=nn.ReLU,
activation: type = nn.ReLU,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we use Type[nn.Module] here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes but I can't get it to work. I may be missing something.

speechbrain/lobes/models/transformer/Transformer.py:228:28 - error: Argument of type "type[Module]" cannot be assigned to parameter "activation" of type "type[ReLU]" in function "__init__"
    "type[Module]" is not assignable to "type[ReLU]"
    Type "type[Module]" is not assignable to type "type[ReLU]" (reportArgumentType)

Copy link
Collaborator

Choose a reason for hiding this comment

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

I messed around with this a bit and still couldn't get it to work, I'm fine with type for now until we find something better.

def forgetMult(self, f, x, hidden):
# type: (Tensor, torch.Tensor, Optional[Tensor]) -> torch.Tensor # noqa F821
def forgetMult(
self, f: torch.Tensor, x: torch.Tensor, hidden: Optional[torch.Tensor]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this be hidden: Optional[torch.Tensor] = None ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No default value was given in the original, and I haven't changed the behaviour. This may have been the original intention, or not. Of course Optional[int] is int | None: Optional does not mean that the argument is optional.

Copy link
Collaborator

@pplantinga pplantinga left a comment

Choose a reason for hiding this comment

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

LGTM

d_ffn=2048,
dropout=0.1,
activation=nn.ReLU,
activation: type = nn.ReLU,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I messed around with this a bit and still couldn't get it to work, I'm fine with type for now until we find something better.

@pplantinga pplantinga merged commit f769926 into speechbrain:develop May 19, 2025
5 checks passed
pplantinga pushed a commit to pplantinga/speechbrain that referenced this pull request Jun 2, 2025
…n#2911)

Co-authored-by: Rogier van Dalen <r.vandalen@samsung.com>
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