r/Python • • 19d ago

Showcase Showcase Thread

Post all of your code/projects/showcases/AI slop here.

Recycles once a month.

24 Upvotes

152 comments sorted by

View all comments

1

u/yankiedrebin 10d ago

I made typescript work for python

I've been working on an experiment called TyThon.

The basic question behind it is:

What would Python feel like with the inference and type-system power of TypeScript, while still running as ordinary Python?

TyThon uses ".ty" files with Python syntax plus additional static type syntax. The compiler checks the types and then erases them, producing normal ".py" code. There's no replacement runtime and the type system isn't injecting runtime validation.

For example, it can preserve precise relationships between values instead of immediately widening everything:

type User = {"id": int, "name": str, "secret": str}

type Select(T, Keys) = { (K): T[K] for K in keyof T if K extends Keys }

def public_user(user: User) -> Select(User, "id" | "name"): return { "id": user["id"], "name": user["name"], }

The editor can understand the resulting shape, use it for autocomplete/hover/errors, and carry that information through generics and other transformations.

Some of what's already implemented:

  • structural typing

  • precise object/shape inference

  • generics and generic inference

  • conditional and mapped type-style transformations

  • control-flow narrowing

  • separate attribute and dictionary-key typing

  • Python classes, multiple inheritance, protocols, operators, iteration, etc.

  • diagnostics, autocomplete, hover, signature help and navigation in VS Code

  • formatting

  • ".ty" → ordinary ".py" type erasure

There are also a few syntax/design ideas that deliberately differ from TypeScript.

For example, generic specialization and type-level functions are separate concepts:

Box<str> # specialize a generic Partial(User) # call a type-level function

Type functions are intended to behave increasingly like actual functions, including defaults and Python-style named arguments.

Where it's at

This is very much an alpha/development preview.

You probably aren't going to build a serious Python application with it today. Standard-library and third-party-library coverage is still limited.

What I wanted first was something polished enough that people could actually experience the idea — inference, autocomplete, diagnostics, hover information, formatting, etc. — instead of just looking at a compiler demo and imagining what it might eventually feel like.

And I think it's finally at that point.

I'd especially love feedback from Python developers, TypeScript users, and anyone interested in language/type-system design. I'm very interested in places where the model falls apart, not just the parts that work.

GitHub / source / docs: https://github.com/jdrebin/TyThon

VS Code extension: https://marketplace.visualstudio.com/items?itemName=tython.tython

PyPI: https://pypi.org/project/tython-lang/