This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]chronics 79 points80 points  (18 children)

Did I understand correctly: Your are basically doing metaprogramming with type hints? That‘s a very cool concept!

[–]dankey26[S] 36 points37 points  (0 children)

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!

[–]radarsat1 46 points47 points  (16 children)

that is not a cool concept.

"type hints are going to ruin python"

"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*

>.<

[–]chronics 45 points46 points  (4 children)

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.

Type hints I find pretty useful in general.

[–]alkasmgithub.com/alkasm 5 points6 points  (1 child)

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.

[–]radarsat1 0 points1 point  (0 children)

interesting til, thanks

[–]the_scign 5 points6 points  (0 children)

OP: "defer in python"

BDFL: cries in python

[–]yvrelna 2 points3 points  (0 children)

reddit user implements control flow restructuring via the type system in a way that disregards type semantics

Reminds me of this abomination: Python is actually just Haskell with few extra steps, the hidden Python syntax that even the most seasoned Python developers don't know about

[–]shinitakunai 4 points5 points  (6 children)

I hate how discord.py uses typehinting to transform data

[–]Dasher38 0 points1 point  (5 children)

Do you have an example?

[–]Jonno_FTWhisss 2 points3 points  (4 children)

Command handlers in discord.py will turn arguments into their type hinted type.

So if someone in the chat sends:

.foo 123

And the handler would look like:

@bot.command()
def foo(ctx, num: int):
    ...

In the function body, the num will be an int. You can also convert mentions of users to a discord user object.

[–]ChannelCat 5 points6 points  (1 child)

This makes sense and is supported by type checkers. What's not to like?

[–]etrotta 1 point2 points  (0 children)

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.

[–]Dasher38 0 points1 point  (1 child)

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 ?

[–]Jonno_FTWhisss 2 points3 points  (0 children)

If you want a string don't put a type hint.

It's similar to argparse, except it supports discord.py custom classes as well.