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

Pyright in core #1939

Merged
merged 18 commits into from
Jan 7, 2022
Merged

Pyright in core #1939

merged 18 commits into from
Jan 7, 2022

Conversation

grdddj
Copy link
Contributor

@grdddj grdddj commented Nov 24, 2021

Creating a PR for the pyright-transition branch on top of mypy-core-testing to be able to track and discuss the changes and to have CI checking all the device tests etc.

@grdddj grdddj requested a review from matejcik November 24, 2021 09:37
core/src/apps/ethereum/tokens.py Outdated Show resolved Hide resolved
core/src/apps/ethereum/tokens.py.mako Outdated Show resolved Hide resolved
@grdddj
Copy link
Contributor Author

grdddj commented Nov 24, 2021

After last commit, pyright is showing around 100 issues (however a lot of them are duplicates). Below they are grouped and commented (some errors could be missing here):

/home/jmusil/trezor-firmware/core/src/usb.py:106:13 - error: "iface_debug" is possibly unbound (reportUnboundVariable)
/home/jmusil/trezor-firmware/core/src/usb.py:108:13 - error: "iface_webauthn" is possibly unbound (reportUnboundVariable)
/home/jmusil/trezor-firmware/core/src/usb.py:110:13 - error: "iface_vcp" is possibly unbound (reportUnboundVariable)
- could just merge the corresponding IFs together (even though the high-level visibility could suffer a little bit)

 /home/jmusil/trezor-firmware/core/src/apps/bitcoin/get_address.py:92:40 - error: "multisig_xpub_magic" is possibly unbound (reportUnboundVariable)
- could be a problem when NOT if coin.segwit and not msg.ignore_xpub_magic:

/home/jmusil/trezor-firmware/core/src/apps/bitcoin/sign_tx/bitcoin.py:573:60 - error: "txi_sign" is possibly unbound (reportUnboundVariable)
/home/jmusil/trezor-firmware/core/src/apps/bitcoin/sign_tx/bitcoin.py:580:27 - error: "txi_sign" is possibly unbound (reportUnboundVariable)
- could be an issue when index will be higher than the length of tx_info.tx.inputs_count

/home/jmusil/trezor-firmware/core/src/apps/webauthn/credential.py:224:31 - error: Argument of type "str" cannot be assigned to parameter "data" of type "bytes | None" in function "__init__"
- str.encode() if it is really a string

/home/jmusil/trezor-firmware/core/src/apps/webauthn/fido2.py:606:39 - error: Function with declared type of "bool | State" must return value
- could raise UnimplementedError

/home/jmusil/trezor-firmware/core/src/trezor/ui/layouts/tt/__init__.py:135:31 - error: Argument of type "str | bytes | None" cannot be assigned to parameter "confirm" of type "str" in function "__init__"
- strange that it coould even be bytes

/home/jmusil/trezor-firmware/core/src/trezor/ui/layouts/tt/__init__.py:135:44 - error: Argument of type "str | bytes | None" cannot be assigned to parameter "cancel" of type "bool" in function "__init__"
- could do bool(xxx)

/home/jmusil/trezor-firmware/core/src/trezor/ui/__init__.py:46:5 - error: Declaration "refresh" is obscured by a declaration of the same name (reportGeneralTypeIssues)
/home/jmusil/trezor-firmware/core/src/trezor/sdcard.py:68:5 - error: Declaration "is_present" is obscured by a declaration of the same name (reportGeneralTypeIssues)
/home/jmusil/trezor-firmware/core/src/trezor/sdcard.py:69:5 - error: Declaration "capacity" is obscured by a declaration of the same name (reportGeneralTypeIssues)
- I see no problem there

/home/jmusil/trezor-firmware/core/src/trezor/utils.py:143:19 - error: Expression of type "str" cannot be assigned to yield type "Chunkable@chunks_intersperse"
- str is defined in Chunkable types

/home/jmusil/trezor-firmware/core/src/trezor/sdcard.py:68:18 - error: "sdcard" is possibly unbound (reportUnboundVariable)
/home/jmusil/trezor-firmware/core/src/trezor/sdcard.py:69:16 - error: "sdcard" is possibly unbound (reportUnboundVariable)
- not a problem

/home/jmusil/trezor-firmware/core/src/apps/common/request_pin.py:110:21 - error: "request_pin_on_device" is possibly unbound (reportUnboundVariable)
- is alright, it must be defined

/home/jmusil/trezor-firmware/core/src/storage/sd_salt.py:84:15 - error: "fatfs" is not a known member of module (reportGeneralTypeIssues)
/home/jmusil/trezor-firmware/core/src/apps/debug/__init__.py:194:16 - error: "sdcard" is not a known member of module (reportGeneralTypeIssues)
/home/jmusil/trezor-firmware/core/src/apps/base.py:43:27 - error: "device" is not a known member of module (reportGeneralTypeIssues)
- strange stuff

/home/jmusil/trezor-firmware/core/src/apps/bitcoin/sign_tx/tx_info.py:24:16 - error: Expression of type "ellipsis" cannot be assigned to declared type "CoinInfo"
- we may just assign something to it

/home/jmusil/trezor-firmware/core/src/apps/bitcoin/sign_tx/helpers.py:255:6 - error: Return type of generator function must be "Generator" or "Iterable" (reportGeneralTypeIssues)
- we may need to change the return type

-------------------------------
MORE COMPLEX ISSUES
-------------------------------

/home/jmusil/trezor-firmware/core/src/apps/bitcoin/sign_tx/helpers.py:184:14 - error: Expression of type "(l: object, r: object) -> bool" cannot be assigned to declared type "(self: Self@object, __o: object) -> bool"
    Type "(l: object, r: object) -> bool" cannot be assigned to type "(self: Self@object, __o: object) -> bool"
      Parameter name mismatch: "self" versus "l" (reportGeneralTypeIssues)
	  
/home/jmusil/trezor-firmware/core/src/trezor/ui/components/tt/word_select.py:55:20 - error: Expression of type "Tuple[Task | Awaitable[Any], ...]" cannot be assigned to return type "tuple[Task, ...]"
    Tuple entry 1 is incorrect type
      Type "Task | Awaitable[Any]" cannot be assigned to type "Task"
        "Awaitable[Any]" is incompatible with "Task" (reportGeneralTypeIssues)
		
/home/jmusil/trezor-firmware/core/src/trezor/ui/components/tt/scroll.py:149:20 - error: Expression of type "tuple[Task, Task, Coroutine[Any, Any, None], Awaitable[Any]]" cannot be assigned to return type "tuple[Task, ...]"
    Tuple entry 4 is incorrect type
      "Awaitable[Any]" is incompatible with "Task" (reportGeneralTypeIssues)
	  
/home/jmusil/trezor-firmware/core/src/apps/bitcoin/sign_tx/__init__.py:70:28 - error: Expression of type "Type[Zcashlike]" cannot be assigned to declared type "Type[SignerClass]"
    "Zcashlike" is incompatible with protocol "SignerClass"
    Type "Type[Zcashlike]" cannot be assigned to type "Type[SignerClass]"
      "__init__" is an incompatible type
        Type "(tx: SignTx, keychain: Keychain, coin: CoinInfo, approver: Approver) -> None" cannot be assigned to type "(tx: SignTx, keychain: Keychain, coin: CoinInfo, approver: Approver | None) -> None"
          Parameter 4: type "Approver | None" cannot be assigned to type "Approver"
            Type "Approver | None" cannot be assigned to type "Approver" (reportGeneralTypeIssues)

/home/jmusil/trezor-firmware/core/src/apps/bitcoin/keychain.py:233:12 - error: Expression of type "(ctx: Context, msg: MsgIn@wrapper, auth_msg: MessageType | None = None) -> Coroutine[Any, Any, MsgOut@with_keychain]" cannot be assigned to return type "Handler[Unknown, MsgOut@with_keychain]"
    Type "(ctx: Context, msg: MsgIn@wrapper, auth_msg: MessageType | None = None) -> Coroutine[Any, Any, MsgOut@with_keychain]" cannot be assigned to type "Handler[Unknown, MsgOut@with_keychain]"
      Parameter 2: type "MsgIn@Handler" cannot be assigned to type "MsgIn@wrapper" (reportGeneralTypeIssues

@grdddj
Copy link
Contributor Author

grdddj commented Nov 25, 2021

Force-pushed rebase on master, done in combination with rebasing the "base" grdddj/mypy_whole_src branch - however, after this it is not correctly showing the difference between these two branches, which is unfortunate.

I still have both original "pre-rebased" branches locally, can I do it somehow better so we will maintain this difference in this PR? (I did the rebases separately for both branches, maybe first rebasing the mypy_whole_src and then this one on top of it?)

@matejcik matejcik changed the base branch from grdddj/mypy_whole_src to master December 1, 2021 12:08
@matejcik
Copy link
Contributor

matejcik commented Jan 5, 2022

zero issues as of now with pyright 1.1.202 (the newest 1.1.203 brings a bug)
let's see what CI has to say

Copy link
Contributor Author

@grdddj grdddj left a comment

Choose a reason for hiding this comment

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

Some smaller comments, otherwise good to go.

General themes:

  • from typing import ... on top-level when it is being used in the code (not only in if TYPE_CHECKING: branch)
    • quite a lot of places in core/src/trezor
  • using xxx | None instead of Optional[xxx]
    • at least two occurrences
  • ctx: Context vs ctx: wire.Context - whether to do from trezor.wire import Context or from trezor import wire
    • but not really important
  • specifying the error being ignored in # type: ignore [error substring]
    • in the whole core folder
  • will we use the pyright-tool?
    • connected with the previous one, and to identify unused ignores

core/pyrightconfig.json Show resolved Hide resolved
core/src/apps/binance/layout.py Outdated Show resolved Hide resolved
core/src/apps/bitcoin/sign_tx/helpers.py Show resolved Hide resolved
core/src/apps/cardano/helpers/bech32.py Show resolved Hide resolved
core/src/apps/eos/actions/__init__.py Show resolved Hide resolved
core/src/trezor/wire/__init__.py Show resolved Hide resolved
core/src/trezor/wire/__init__.py Outdated Show resolved Hide resolved
core/src/trezor/wire/codec_v1.py Show resolved Hide resolved
core/src/trezor/workflow.py Show resolved Hide resolved
core/tests/test_trezor.protobuf.py Show resolved Hide resolved
@grdddj
Copy link
Contributor Author

grdddj commented Jan 7, 2022

In this branch, core contains 94 # type: ignore comments, but when I disabled them by "enableTypeIgnoreComments": false in pyrightconfig.json, only 51 errors were encountered.

That means we could get rid of a lot of these # type: ignores. We could also take the opportunity to put the error message in the ignore code in errors which persist. core/src/apps/bitcoin/sign_tx/helpers.py also contains around 20 of the exact same issues, so it is a good candidate for a pyright-tool's FILE_SPECIFIC_IGNORES, not to duplicate the ignore so many times

Probably as a first step, we should try to resolve at least some of those 51 remaining issues

docs/core/misc/codestyle.md Outdated Show resolved Hide resolved
@matejcik
Copy link
Contributor

matejcik commented Jan 7, 2022

TODO: at rebase time, add "[no changelog]" to 5ca6b81

@grdddj
Copy link
Contributor Author

grdddj commented Jan 7, 2022

Seems good to me! Looking forward to rebasing the pyright-tool PR on these things in master

@matejcik matejcik merged commit 4ab33f0 into master Jan 7, 2022
@matejcik matejcik deleted the grdddj/pyright_in_core branch January 7, 2022 20:41
@prusnak
Copy link
Member

prusnak commented Jan 7, 2022

Love this PR! Python 3.10 type annotations are great! 👍

@jimpo
Copy link

jimpo commented Jan 9, 2022

This seems to have broken the docker image because the image does not COPY in the new pyright directory

prusnak added a commit that referenced this pull request Jan 9, 2022
@prusnak
Copy link
Member

prusnak commented Jan 9, 2022

@jimpo thanks for the notice! fix in #2054

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.

None yet

5 participants