enforce
cinder
enforce | cinder | |
---|---|---|
3 | 43 | |
541 | 3,379 | |
- | 0.5% | |
0.0 | 9.4 | |
about 2 years ago | about 22 hours ago | |
Python | Python | |
- | GNU General Public License v3.0 or later |
Stars - the number of stars that a project has on GitHub. Growth - month over month growth in stars.
Activity is a relative number indicating how actively a project is being developed. Recent commits have higher weight than older ones.
For example, an activity of 9.0 indicates that a project is amongst the top 10% of the most actively developed projects that we are tracking.
enforce
-
Python’s “Type Hints” are a bit of a disappointment to me
Every point in this blog post strikes me as either (1) unaware of the tooling around python typing other than mypy, or (2) a criticism of static-typing-bolted-on-to-a-dynamically-typed-language, rather than Python's hints. Regarding (1), my advise to OP is to try out Pyright, Pydantic, and Typeguard. Pyright, especailly, is amazing and makes the process of working with type hints 2 or 3 times smoother IMO. And, I don't think points that fall under (2) are fair criticisms of type *hints*. They are called hints for a reason.
Otherwise, here's a point-by-point response, either recommending OP checks out tooling, or showing that the point being made is not specific to Python.
> type hints are not binding.
There are projects [0][1] that allow you to enforce type hints at runtime if you so choose.
It's worth mentioning that this is very analogous to how Typescript does it, in that type info is erased completely at runtime.
> Type checking is your job after all, ...[and that] requires maintenance.
There are LSPs like Pyright[2] (pyright specifically is the absolute best, IMO) that report type errors as you code. Again, this is very very similar to typescript.
> There is an Any type and it renders everything useless
I have never seen a static-typing tool that was bolted on to a dynamically typed language, without an `Any` type, including typescript.
> Duck type compatibility of int and float
The author admits that they cannot state why this behavior is problematic, except for saying that it's "ambiguous".
> Most projects need third-party type hints
Again, this is a criticism of all cases where static types are bolted on dynamically typed languages, not Python's implementation specifically.
> Sadly, dataclasses ignore type hints as well
Pydantic[3] is an amazing data parsing library that takes advantage of type hints, and it's interface is a superset of that of dataclasses. What's more, it underpins FastAPI[4], an amazing API-backend framework (with 44K Github stars).
> Type inference and lazy programmers
The argument of this section boils down to using `Any` as a generic argument not being an error by default. This is configurable to be an error both in Pyright[5], and mypy[6].
> Exceptions are not covered [like Java]
I can't find the interview/presentation, but Guido Van Rossum specifically calls out Java's implementation of "exception annotations" as a demonstration of why that is a bad idea, and that it would never happen in Python. I'm not saying Guido's opinion is the absolute truth, but just letting you know that this is an explicit decision, not an unwanted shortcoming.
[0] https://github.com/RussBaz/enforce
[1] https://github.com/agronholm/typeguard
[2] https://github.com/microsoft/pyright
[3] https://pydantic-docs.helpmanual.io
[4] https://github.com/tiangolo/fastapi
[5] https://github.com/microsoft/pyright/blob/main/docs/configur...
[6] https://mypy.readthedocs.io/en/stable/config_file.html#confv...
-
Unit tests & type hinting
Not by default. But there are libraries to enforce types. https://github.com/RussBaz/enforce or/and https://pydantic-docs.helpmanual.io/
- Type validation decorator
cinder
-
Meta Used Monolithic Architecture to Ship Threads in Only Five Months
Meta is actually contributing directly to upstream cpython. If you really wanted to, the internal fork is also open source: https://github.com/facebookincubator/cinder
- Meta pledges Three-Year sponsorship for Python if GIL removal is accepted
- Back end of Meta Threads is built with Python 3.10 with some interesting tweaks
-
Lessons from Mojo for PHP 10+ ?
Just one example: last year Meta open-sourced Cinder, which powers Instagram and provides sizeable speedups compared to CPython.
- Python true static typing
-
Best book on writing an optimizing compiler (inlining, types, abstract interpretation)?
I used to work on the Cinder JIT and can help document any passes you find interesting or confusing.
-
Python-based compiler achieves orders-of-magnitude speedups
You might enjoy Cinder then. It's based on CPython so it is nearly 100% compatible.
https://github.com/facebookincubator/cinder/
Disclaimer: I used to work on it.
-
beartype: It has documentation now. It only took two years, my last hair follicle, precious sanity points (SPs), and working with Sphinx. Don't be like @leycec. Go hard on documentation early.
I think Cinder's Static Python, which also performs runtime type checking, is more ambitious. Though it's not production ready yet.
-
If there’s gonna be a Python 4.0 one day, what’s a breaking change you’d like to see? Let’s explore the ideas you have that can make Python even better!
Here's a fork that implements that https://github.com/facebookincubator/cinder - it might be nice to one day get that up streamed but obviously it'll be controversial and it certainly needs more time to bake. Hopefully at some point we can make it a pip installable extension though.
-
Is it time for Python to have a statically-typed, compiled, fast superset?
The other thing that was interesting to me, was the potential of type annotations to help make for a faster, safer experience on the compiler end of things. One example is seen in Meta’s Cinder project, on the docs it explains how typing can be used to reduce the number of steps for the compiler ([cinder/static_python.rst at cinder/3.8 · facebookincubator/cinder · GitHub](https://github.com/facebookincubator/cinder/blob/cinder/3.8/CinderDoc/static_python.rst)), making it more effective.
What are some alternatives?
pydantic - Data validation using Python type hints
faster-cpython - How to make CPython faster.
pydantic-to-typescript - CLI Tool for converting pydantic models into typescript definitions
Pyjion - Pyjion - A JIT for Python based upon CoreCLR
Nim - Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
Pyjion
streamlit-pydantic - 🪄 Auto-generate Streamlit UI from Pydantic Models and Dataclasses.
graalpython - A Python 3 implementation built on GraalVM
libsa4py - LibSA4Py: Light-weight static analysis for extracting type hints and features
MonkeyType - A Python library that generates static type annotations by collecting runtime types
pyright - Static Type Checker for Python
hpy - HPy: a better API for Python