this post was submitted on 19 Jun 2025
366 points (100.0% liked)

Programmer Humor

25026 readers
1360 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
366
submitted 3 weeks ago* (last edited 3 weeks ago) by [email protected] to c/[email protected]
 

Made with KolourPaint and screenshots from Kate (with the GitHub theme).

you are viewing a single comment's thread
view the rest of the comments
[โ€“] [email protected] 1 points 3 weeks ago (1 children)

dataclasses do this for you at the class level. They enforce type annotations at instantiation.

https://docs.python.org/3/library/dataclasses.html

[โ€“] [email protected] 1 points 3 weeks ago* (last edited 3 weeks ago)

i think we're talking about different things. you use enforce to mean "validate", i used it to mean "coerce". one of the cases was a command line argument parser that consisted of a single decorator, so you could write


@command
def foo(bar: int, baz: float):
    print(baz * 2 + bar * 3)

and call it with $ myfile.py foo --bar 3 --baz 2.2 and it would print 13.4

another was about creating working protocol buffers from an excel sheet, nested types and enums and oneofs and everything. we used it to parameterize tests of our bluetooth protocol.