by phailhaus · 3y ago · 0 comments
I don't understand this, what do you mean? TypeScript is such a success partly because everything is an object in Javascript. There's no need to distinguish between classes and "dicts", as you have to with Python. Another example: Python functions have args and kwargs, which make typing significantly more complex (as the article points out, Callable doesn't even support kwargs). Javascript doesn't, so typing functions is trivial.
Part of the point of static typechecking is documentation that can be verified by tooling. If you have no types in your codebase, and you decide to change something from a dict to a class, good luck! You're going to have to manually trace all the data flows through your entire application to figure out what's affected.
Everything is an object in Python too.
> There's no need to distinguish between classes and "dicts", as you have to with Python.
"dict" in Python (or at least Python 3, but Python 2 is EOL now so Python 3 is the only active Python there is) is a class. You can even subclass it, which if you want a customized dict for some reason in your particular application is often the best way to do it. Or you can subclass MutableMapping, which dict itself is a subclass of, and which is what is supposed to be used for isinstance checks now that Python has established abstract base classes for all of its standard "built-in type" interfaces.
Ah yes, true. JavaScript goes one step further though, and has no distinction between get-item accesses and attribute accesses. This makes TypeScript's data model much simpler to manage. Is it even possible to implement your own `MutableMapping` or `dataclasses` equivalent type in Python? I know the latter requires custom plugins.
Can you explain a bit more about what you mean here?
> Is it even possible to implement your own `MutableMapping` or `dataclasses` equivalent type in Python?
Of course. With dataclasses that's what Python itself does; that's a pure Python module:
https://github.com/python/cpython/blob/3.11/Lib/dataclasses....
For MutableMapping, Python currently implements it as a C class for speed, but you could implement the same functionality in pure Python. That's what earlier versions of the collections.abc module did.
> I know the latter requires custom plugins.
I don't know what you mean here. See the pure Python module that Python itself provides above.
> Of course. With dataclasses that's what Python itself does; that's a pure Python module:
Sorry, I meant that you can't create an equivalent class that typechecks the same way. For example, when you define a dataclass with fields, mypy and pyright are capable of inferring what args and kwargs the constructor supports. But that's because it was implemented as a special case. For example, see the mypy docs [1]. If you just alias `dataclasses`, you lose type support!
Pydantic is a library with similar semantics, and the only way to get typechecking support is to install the pydantic plugin for the typechecker you use. The language simply can't express it.
[1] https://mypy.readthedocs.io/en/stable/additional_features.ht...
Not really. It's only complex when they're used with poor design decisions. There are plenty of languages that allow you to do things that aren't a good idea. Python is no exception. Type hinting just makes those shortcomings more readily apparent.
> Callable doesn't even support kwargs
PEP 544 and Protocols have existed for 5 years, and they support keyword only type hinting for callables.
Expecting to inherit type hints from an unrelated place, like the constructor of a superclass, is quite unjustified. The superclass is an implementation detail.
And while that is a simple pattern, it's not a good pattern, and quickly becomes unmaintainable in a program of any size. Using *kwargs because of Too-many-arguments and so you can violate the Liskov substitution principle isn't good programming. Just because it can be done doesn't mean we should start bastardizing typing to make it easier.
I don't think so. I just threw together a simple example that doesn't work:
from typing import TypedDict
class A(TypedDict):
name: str
age: int
def myfunc(**kwargs: A):
print(kwargs)
myfunc()
No errors with mypy. In fact, kwargs is inferred as `dict[str, A]`, which is unbelievable given that Python explicitly supports `**kwargs` as a way to represent a grab-bag of keyword arguments.> And while that is a simple pattern, it's not a good pattern, and quickly becomes unmaintainable in a program of any size
It's not though, if you had a proper type system. For example, you could do this trivially in Typescript and it would automatically infer the types of the rest of the kwargs. That means that your type information is preserved, making it possible to keep using your subclass without losing information about the total set of arguments it supports. The advantage is that you can add/remove arguments to your base class without having to hunt down every subclass, and you're guaranteed to be correct (and it would error otherwise!).