For the code completion and type hinting in IDEs, just add static typing for the Person and Address classes and you are already good to go. Assuming you use the latest python3.6, here's a rough equivalent of the typescript classes from your example:
# spam.py
from typing import Optional, Sequence
class Address:
street: str
housenumber: int
housenumber_postfix: Optional[str]
def __init__(self, street: str, housenumber: int,
housenumber_postfix: Optional[str] = None) -> None:
self.street = street
self.housenumber = housenumber
self.housenumber_postfix = housenumber_postfix
class Person:
name: str
adresses: Sequence[Address]
def __init__(self, name: str, adresses: Sequence[str]) -> None:
self.name = name
self.adresses = adresses
person = Person('Joe', [
Address('Sesame', 1),
Address('Baker', 221, housenumber_postfix='b')
]) # type: Person
I suppose the boilerplate you mentioned emerges when adding the class constructors. This is indeed inavoidable. I would wish default constructors were generated at runtime when not declared explicitly, like this:
class Address:
street: str
housenumber: int
housenumber_postfix: Optional[str]
class Person:
name: str
adresses: Sequence[Address]
if __name__ == '__main__':
alice = Person('Alice', [Address('spam', 1, housenumber_postfix='eggs')])
bob = Person('Bob', ()) # a tuple is also a sequence
but unfortunately you have to declare them manually.
Edit
As Michael0x2a pointed out in the comment, the need for default constructors is made avoidable in python3.7 which introduced a @dataclass decorator, so one can indeed declare:
@dataclass
class Address:
street: str
housenumber: int
housenumber_postfix: Optional[str]
@dataclass
class Person:
name: str
adresses: Sequence[Address]
and get the default impl of several methods, reducing the amount of boilerplate code. Check out PEP 557 for more details.
I guess you could see stub files that can be generated from your code, as some kind of interface files:
$ stubgen spam # stubgen tool is part of mypy package
Created out/spam.pyi
The generated stub file contains the typed signatures of all non-private classes and functions of the module without implementation:
# Stubs for spam (Python 3.6)
#
# NOTE: This dynamically typed stub was automatically generated by stubgen.
from typing import Optional, Sequence
class Address:
street: str
housenumber: int
housenumber_postfix: Optional[str]
def __init__(self, street: str, housenumber: int, housenumber_postfix: Optional[str]=...) -> None: ...
class Person:
name: str
adresses: Sequence[Address]
def __init__(self, name: str, adresses: Sequence[str]) -> None: ...
person: Person
These stub files are also recognized by IDEs and if your original module is not statically typed, they will use the stub file for type hints and code completion.
Answer from hoefling on Stack OverflowPython equivalent of Typescript interface - Stack Overflow
arguments on why the team should use typescript instead of python?
python developer on a typescript project
Python dev looking to learn typescript
Videos
Hey there, I'm looking to learn typescript. I´m decent with python and coding concepts in general, so I'm looking for resources that allow me to leverage that (e.g. X in typescript is comparable to Y in python). Can you recommend me some books, articles or videos/youtube channels?
For the code completion and type hinting in IDEs, just add static typing for the Person and Address classes and you are already good to go. Assuming you use the latest python3.6, here's a rough equivalent of the typescript classes from your example:
# spam.py
from typing import Optional, Sequence
class Address:
street: str
housenumber: int
housenumber_postfix: Optional[str]
def __init__(self, street: str, housenumber: int,
housenumber_postfix: Optional[str] = None) -> None:
self.street = street
self.housenumber = housenumber
self.housenumber_postfix = housenumber_postfix
class Person:
name: str
adresses: Sequence[Address]
def __init__(self, name: str, adresses: Sequence[str]) -> None:
self.name = name
self.adresses = adresses
person = Person('Joe', [
Address('Sesame', 1),
Address('Baker', 221, housenumber_postfix='b')
]) # type: Person
I suppose the boilerplate you mentioned emerges when adding the class constructors. This is indeed inavoidable. I would wish default constructors were generated at runtime when not declared explicitly, like this:
class Address:
street: str
housenumber: int
housenumber_postfix: Optional[str]
class Person:
name: str
adresses: Sequence[Address]
if __name__ == '__main__':
alice = Person('Alice', [Address('spam', 1, housenumber_postfix='eggs')])
bob = Person('Bob', ()) # a tuple is also a sequence
but unfortunately you have to declare them manually.
Edit
As Michael0x2a pointed out in the comment, the need for default constructors is made avoidable in python3.7 which introduced a @dataclass decorator, so one can indeed declare:
@dataclass
class Address:
street: str
housenumber: int
housenumber_postfix: Optional[str]
@dataclass
class Person:
name: str
adresses: Sequence[Address]
and get the default impl of several methods, reducing the amount of boilerplate code. Check out PEP 557 for more details.
I guess you could see stub files that can be generated from your code, as some kind of interface files:
$ stubgen spam # stubgen tool is part of mypy package
Created out/spam.pyi
The generated stub file contains the typed signatures of all non-private classes and functions of the module without implementation:
# Stubs for spam (Python 3.6)
#
# NOTE: This dynamically typed stub was automatically generated by stubgen.
from typing import Optional, Sequence
class Address:
street: str
housenumber: int
housenumber_postfix: Optional[str]
def __init__(self, street: str, housenumber: int, housenumber_postfix: Optional[str]=...) -> None: ...
class Person:
name: str
adresses: Sequence[Address]
def __init__(self, name: str, adresses: Sequence[str]) -> None: ...
person: Person
These stub files are also recognized by IDEs and if your original module is not statically typed, they will use the stub file for type hints and code completion.
A TypeScript interface describes a JavaScript object. Such an object is analogous to a Python dictionary with well-known string keys, which is described by a TypedDict.
TypeScript interface example
For example the TypeScript interface:
interface Address {
street: string;
housenumber: number;
}
will describe JavaScript objects like:
var someAddress = {
street: 'SW Gemini Dr.',
housenumber: 9450,
};
Python TypedDict example
The equivalent Python TypedDict:
from typing import TypedDict
class Address(TypedDict):
street: str
housenumber: int
will describe Python dictionaries like:
some_address = {
'street': 'SW Gemini Dr.',
'housenumber': 9450,
}
# or equivalently:
some_address = dict(
street='SW Gemini Dr.',
housenumber=9450,
)
These dictionaries can be serialized to/from JSON trivially and will conform to the analogous TypeScript interface type.
Note: If you are using Python 2 or older versions of Python 3, you may need to use the older function-based syntax for TypedDict:
from mypy_extensions import TypedDict
Address = TypedDict('Address', {
'street': str,
'housenumber': int,
})
Alternatives
There are other ways in Python to represent structures with named properties.
Data classes, available in Python 3.7, have read-write keys. However they cannot be serialized to/from JSON automatically.
from dataclasses import dataclass
@dataclass
class Address:
street: str
housenumber: int
my_address = Address(
street='SW Gemini Dr.',
housenumber=9450,
)
Named tuples are cheap and have read-only keys. They also cannot be serialized to/from JSON automatically.
from typing import NamedTuple
class Address(NamedTuple):
street: str
housenumber: int
my_address = Address(
street='SW Gemini Dr.',
housenumber=9450,
)
Simple namespaces, available in Python 3.3, are similar to data classes but are not very well known.
from types import SimpleNamespace
class Address(SimpleNamespace):
street: str
housenumber: int
my_address = Address(
street='SW Gemini Dr.',
housenumber=9450,
)
attrs is a long-standing third-party library that is similar to data classes but with many more features. attrs is recognized by the mypy typechecker.
import attrs
@attr.s(auto_attribs=True)
class Address:
street: str
housenumber: int
my_address = Address(
street='SW Gemini Dr.',
housenumber=9450,
)
Hi everyone, I have +/- 4 years of experience with typescript, and recently I've had to work in AWS with python code written by people who are not with the team anymore and who weren't programmers by heart. At first, I was excited to focus on python because it's the fastest and cheapest runtime to use in what we do. I have since become the main developer of the team and constantly push for doing better. However, I have been unable to translate the same high-quality standards that I set for myself to python. I feel like the python ecosystem leaves a lot to be desired (package management, linting configuration, automated testing, strict typing, etc). It's in there, but not nearly as good as in the TS/JS/npm ecosystem.
I personally feel like typescript (and npm) could give us a lot of guardrails to help us help ourselves long term to massively improve our code quality.
I'm a bit nervous (because I really hope we can switch to typescript for future projects) and I'm looking for arguments to make during my pitch. Does anyone have any ideas?
p.s. the speed and cost arguments for python are irrelevant according to my manager