py-evm icon indicating copy to clipboard operation
py-evm copied to clipboard

Several decorators entirely destroy type support for the decorated functions

Open cburgdorf opened this issue 7 years ago • 0 comments

What is wrong?

This is somewhat related to #1272 but yet different enough to create a separate issue about it.

The revealed type of a function such as LightPeerChain.get_block_header_by_hash is currently Any. This means, we can call it as LightPeerChain.get_block_header_by_hash('foobar), LightPeerChain.get_block_header_by_hash(1, 2, 3) or in any other way without mypy noticing anything at all.

Let's take a look at the function definiton.

@alru_cache(maxsize=1024, cache_exceptions=False)
@service_timeout(COMPLETION_TIMEOUT)
async def get_block_header_by_hash(self, block_hash: Hash32) -> BlockHeader:
    ...

There are several problems, one is further outlined in #1272. Let's remove the decorators and notice what mypy things about it then.

Turns out, once we remove the decorators the revealed type becomes def (self: trinity.sync.light.service.LightPeerChain, block_hash: Any) -> typing.Coroutine[Any, Any, eth.rlp.headers.BlockHeader]

The problem here is that the decorators redefine the function without type information. This has already been discussed here once.

How can it be fixed

Do one of the following things:

  • either create type stubs

or

  • dive into PEP561 and https://mypy.readthedocs.io/en/latest/installed_packages.html and see if we can get type safety for external libs without stubs by now.

cburgdorf avatar Sep 10 '18 14:09 cburgdorf