ye. i thought about it for a couple days after the guy suggested it, tried to come up with syntax that could be as clean and close to the go version and a valid parse with no preprocessing on the source code. then remembered that fact on type hints and went for it. turned out not bad!
"oh its fine look they don't even do anything and look at all these nice warnings. they help!"
"but now i have to write twice as much code"
"well you don't have to, they don't even do anything they're just for decoration!"
"ok but now i see that people are using them to generate database schemas and automatic validation and testing, are they still optional? because i mean if they're not optional I'd really like to improve..."
"no don't worry they still don't do anything that's just for testing"
*reddit user implements control flow restructuring via the type system in a way that disregards type semantics*
I think it is an amazing concept because I think metaprogramming in languages like lisp/clojure or erlang/elixir is very powerful. It’s not part of idiomatic python, for good reasons.
OP’s implementation is creative and clever. I would not use it in my own code, and I don‘t think it‘s intended to be used.
Reddit Moderation makes the platform worthless. Too many rules and too many arbitrary rulings. It's not worth the trouble to post. Not worth the frustration to lurk. Goodbye.
This post was mass deleted and anonymized with Redact
Sorry for the pedantry up front but I think it's worth it here here: var: int is an "annotation", which is part of Python 3 syntax, and not part of the type system. Annotations existed before type checkers were used. It was assumed of course that they would be used for type information, but they also wanted to leave the syntax separated from a specific use case to see what other patterns may emerge from it, though as we know today it is only really used for type checking and for runtime type info in certain libraries. But just pointing out OP isn't abusing the type system, OP is just experimenting with a DSL utilizing the annotation syntax.
one can argue that it is intrinsically wrong for type hints (which are metadata, much like comments) to modify the program's behavior at all, but in reality nearly everyone is fine with it since it does makes sense in cases like this one and other libraries that parse structured input in similar ways.
I see, it's basically like the argparse "type" parameter. Isn't it convenient ? If you want to get the input unchanged you can always use str isn't it ?
It is occasionally useful, if you need to close a large or variable number of things in a block of code. Fortunately, contextlib.ExitStack is already in the stdlib and is a less evil way to do this.
It's "defer". Defer in languages like Go causes its associated statement to be executed at function exit (i.e. it is deferred until later), whether that function exits by returning or exception/panic, or whatever means. It's like a try...finally block around the remainder of the function. The idea is that you keep functions looking slightly cleaner, keeps resource init and deinit code together, so you don't have to keep track of cleanup code over the rest of the function as it evolves, updating indentation (or brackets in other languages), etc.
So if Python had a defer statement it might look something like this:
Python essentially has a defer statement and it looks like this:
from contextlib import closing
def my_function(x, y):
with closing(thing(x)) as X, closing(thing(y)) as Y:
do_something_with_thing(X)
do_something_with_thing(Y)
Where the context manager ensures things get closed at the end of the context. Pretty much equivalent to your second code block.
I have done a lot of meta-programming in this language, but I have not considered using type hints for shenanigans. That's fantastic. I'm gonna have to keep that in mind.
Reddit Moderation makes the platform worthless. Too many rules and too many arbitrary rulings. It's not worth the trouble to post. Not worth the frustration to lurk. Goodbye.
This post was mass deleted and anonymized with Redact
82
u/chronics Nov 25 '22
Did I understand correctly: Your are basically doing metaprogramming with type hints? That‘s a very cool concept!
8
u/wineblood Nov 25 '22
Why would I ever want this?
87
u/Beliskner64 Nov 25 '22
assert len(tree.body) == 1 and isinstance(tree.body[0], ast.FunctionDef), "should just be a function wtf"Genius
42
u/Setepenre Nov 25 '22
The python way would be to use
withwhich is the construct that guarantees resources get freed no matter what.56
u/[deleted] Nov 25 '22
Horrifying yet instructive at the same time. Impressive and yet has no practical use.
I love stuff like this - have an upvote!
45
u/[deleted] Nov 25 '22
The pythonic way of doing deferred cleanup is with a context manager ("with" keyword). This is a pretty cool abuse of syntax though!
11
u/end_my_suffering44 Nov 25 '22 edited Nov 25 '22
Could someone enlighten me what defer does or its purpose?
Edit : Typo
7
u/james_pic Nov 25 '22
If you want a less evil version of the same thing,
contextlib.ExitStackis in the standard library.Edit: just realised it uses ExitStack internally anyway.
14
u/KingsmanVince pip install girlfriend Nov 25 '22
I like the QA section.
8
u/SittingWave Nov 25 '22
the horror....
the horror...
4
u/xakkap Nov 25 '22
Cool stuff
2
u/Coretaxxe Nov 25 '22
Im gonna use this for fun projects. This is awesome!
2
u/chub79 Nov 25 '22
I find the contextmanager syntax so much more pleasing than defering. But I like the explicit aspect of deferring. I'm torn.
1
u/Exodus111 Nov 25 '22
I've never used Go, what would be the reason to use something like this?
1
u/dimonoid123 Nov 25 '22
Just use asyncio at this point
1
u/Stenbyggare Nov 26 '22
that doesn't look really pythonic in my opinion, good concept though
1
u/OkProfessional8364 Nov 26 '22
I'm so lost. I can't make sense of whatever you're all talking about
1
u/Ezlike011011 Nov 26 '22
I have done a lot of meta-programming in this language, but I have not considered using type hints for shenanigans. That's fantastic. I'm gonna have to keep that in mind.
1
u/DeathDaNoob Nov 26 '22
What does defer do...
1
u/Tintin_Quarentino Nov 26 '22
Your website is down?
1
u/[deleted] Nov 25 '22
I love it lol
-13
u/[deleted] Nov 25 '22 edited Feb 05 '24
Reddit Moderation makes the platform worthless. Too many rules and too many arbitrary rulings. It's not worth the trouble to post. Not worth the frustration to lurk. Goodbye.
This post was mass deleted and anonymized with Redact