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

Fix function type signatures #36

Merged
merged 4 commits into from
May 12, 2024

Conversation

akiym
Copy link
Contributor

@akiym akiym commented May 12, 2024

Fix function type signatures

What's this Pull Request for?

Choose one or more of the following items.

  • Bug fix
  • Add/Remove feature
  • Cleaning code (including optimization/typo fix)
  • Others

Please explain the detail here:

  • Breaking change: u32 and u64 now return as int in the type signature
    • Those functions were not able to infer the return value correctly, see also e81615b
  • Fixed type signatures for these functions: chunks, flat, u32, u64, u32f, u64f
    • This resolves the following type errors when using IDE such as IntelliJ IDEA and PyCharm

What have you done so far?

Tell me what you've tested to assure your change is right.

  • All tests passed on your machine (python -m unittest)
  • Added test cases for new feature
  • Nothing / Test not required

(It's not mandatory to check even one of them, but test cases make it easy for the author to review your PR.)

Comment

akiym added 4 commits May 12, 2024 10:07
This change suppresses the following mypy errors:

ptrlib/binary/packing/chunks.py:29: error: Unsupported operand types for * ("_U" and "int")  [operator]
ptrlib/binary/packing/chunks.py:29: note: Left operand is of type "_T | _U"

But the following mypy errors remain:

ptrlib/binary/packing/chunks.py:25: error: Argument 1 to "len" has incompatible type "_T"; expected "Sized"  [arg-type]
ptrlib/binary/packing/chunks.py:26: error: Value of type "object" is not indexable  [index]
ptrlib/binary/packing/chunks.py:28: error: Argument 1 to "len" has incompatible type "_T"; expected "Sized"  [arg-type]
ptrlib/binary/packing/chunks.py:29: error: Unsupported operand types for * ("_T" and "int")  [operator]
ptrlib/binary/packing/chunks.py:29: error: Argument 1 to "len" has incompatible type "_T"; expected "Sized"  [arg-type]
This change suppresses the following mypy errors:

ptrlib/binary/packing/flat.py:12: error: Overloaded function implementation does not accept all possible arguments of signature 1 [misc]
ptrlib/binary/packing/flat.py:24: error: Argument 1 has incompatible type "list[_T] | _T"; expected "_T"  [arg-type]
ptrlib/binary/packing/flat.py:26: error: No overload variant of "__add__" of "list" matches argument type "_T"  [operator]
ptrlib/binary/packing/flat.py:26: note: Possible overload variants:
ptrlib/binary/packing/flat.py:26: note:     def __add__(self, list[_U], /) -> list[_U]
ptrlib/binary/packing/flat.py:26: note:     def [_S] __add__(self, list[_S], /) -> list[_S | _U]
ptrlib/binary/packing/flat.py:26: note:     def __add__(self, list[_T], /) -> list[_T]
ptrlib/binary/packing/flat.py:26: note:     def [_S] __add__(self, list[_S], /) -> list[_S | _T]
ptrlib/binary/packing/flat.py:26: error: Unsupported left operand type for + ("_T")  [operator]
ptrlib/binary/packing/flat.py:26: note: Both left and right operands are unions
ptrlib/binary/packing/flat.py:26: error: Incompatible types in assignment (expression has type "list[_U] | list[_T | _U] | Any | list[_T]", variable has type "list[_U] | list[_T] | _T")  [assignment]
ptrlib/binary/packing/flat.py:26: error: Argument 1 has incompatible type "list[_T] | _T"; expected "_T"  [arg-type]
ptrlib/binary/packing/flat.py:28: error: Incompatible return value type (got "list[_U] | list[_T] | _T", expected "list[_T] | list[_U]") [return-value]

But the following mypy errors remain:

ptrlib/binary/packing/flat.py:26: error: Unsupported left operand type for + ("object")  [operator]
ptrlib/binary/packing/flat.py:28: error: Incompatible return value type (got "object", expected "_T | _U")  [return-value]
mypy can't infer that the return value should be int in the following
code, even if using overload.

def u32(..., result_type: Type[_T]=int) -> _T:

Since result_type is already deprecated and u32/u64 return as int in
many cases, change type signatures for convenience.
@akiym akiym changed the title Fix function signature Fix function type signatures May 12, 2024
@ptr-yudai
Copy link
Owner

Thanks for the PR!
LGTM👌

@ptr-yudai ptr-yudai merged commit 7e2b324 into ptr-yudai:master May 12, 2024
10 checks passed
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