PyRight and type hinting
Strong and weak typed languages
Python is a weakly typed language.
This feature could be a vantage when the codebase is small, but it becames an anti-pattern as project grows.
Nowadays an IDE with a strongly typed language could help the developer a lot suggesting method names and descriptions, variables and so on. A code generator LLM could also have great benefit from typing definition.
To overcome this problem, the PEP 484 introduced non-mandatory syntax extension that permits to declare type hints associated with methods and variables.
Pyright and NeoVim
Pyright is an LSP (Language Server Protocol) for the python language. It is written in nodejs and is usually integrated in the VS Code Python Extension.
NeoVim developers had the great idea to support the LSP interface, and now PyRight is wonderfully integrated with NeoVim, as well as tens of other LSP.
Type hinting and python 2
Unfortunately, python2 has no code extension to specify the typing hitns, but luckily these can be enbedded in comments as described here:
https://peps.python.org/pep-0484/#suggested-syntax-for-python-2-7-and-straddling-code
Type hinting problem
Let’s start with an example:
class OtherClass(object):
def method(self):
return '12345'
class MyClass(object):
def __init__(self):
self.variable = None
def setup(self):
self.variable = OtherClass()
def run(self):
self.variable.method()