kaitai-io / kaitai_struct

Kaitai Struct: declarative language to generate binary data parsers in C++ / C# / Go / Java / JavaScript / Lua / Nim / Perl / PHP / Python / Ruby
https://kaitai.io
4.01k stars 197 forks source link

Generate type hints for python #675

Open KOLANICH opened 4 years ago

KOLANICH commented 4 years ago

Python 3 (2 is EOL) allows to add some type hints. They are not enforced without special measures, but can be used by linters (like mypy), REPLs and langservers.

  1. import typing to the top of a file
  2. Output type of a function is shown by an -> before :
  3. Types of arguments are shown by adding : TypeOfArg right after arg name
  4. Union types are typing.Union[T1, T2]
  5. Lists are typing.List[T], or maybe even more generic typing.Iterable[T]
  6. type of self is usually not marked as a type of an arg
  7. If a func returns nothing, use -> None
  8. You need to set the types as strings containing their names if the type is not yet defined:
def a(b: "A") -> "A":
  return b
class A:
  def __init__(self, asdf: typing.Optional["A"] = None) -> None:
    pass
class B:
  def __init__(self, asdf: A) -> None:
    pass
GreyCat commented 4 years ago

Seems to make sense, likely a good idea. Probably requires certain command line switch to activate to these features.

dgelessus commented 4 years ago

FYI, if Python 2 compatibility is a concern, there are also type comments, which are compatible with both Python 2 and 3.

I don't know if that is relevant for ksc - it depends on how long/much you want to keep support for Python 2. A lot of developers are using only Python 3, all current major Linux distros have moved to Python 3 by default, and libraries have already started dropping Python 2 before its official end of life. Some enterprise environments and existing codebases can be slow to migrate though. (In any case, I'd guess that dropping Python 2 would only happen after 0.9 is released.)

KOLANICH commented 4 years ago

As I have already said, I am using python 3 exclusively from 2013. I mean I used python 2 only on rare occasions when I wanted to merge my patch into an upstream and the upstream strictly required the stuff being working on 2, so I had to use six and make sure it works on both.

GreyCat commented 4 years ago

I don't have any strong stakes related to Python, I just wanted to point that this:

all current major Linux distros have moved to Python 3 by default

is not really true:

dgelessus commented 4 years ago

OK, I should have said "all current Linux distros are moving to Python 3 by default" :) OpenSUSE seems to be a bit slower than Debian, Ubuntu and RHEL, but they have also started moving to Python 3. There's nothing that can be done about old LTS versions of course, those are stuck with whatever Python versions they came with. In any case, Python 3 has been widely available on Linux for a while, even if it wasn't the system default, so it's mostly up to developers and users to choose.

btw, the python in Debian/Ubuntu is just a naming thing. As I understand it, python always implicitly refers to Python 2 (e. g. there are packages python-kaitaistruct and python3-kaitaistruct, but no python2-kaitaistruct), and once Debian has finished removing Python 2 packages, only the python3 versions will be left.