5 ms·
This is the largest codebase I've seen that makes extensive use of the new Python match: statement introduced in Python 3.10 - example: https://github.com/dosis
by simonw 4y ago
This is the largest codebase I've seen that makes extensive use of the new Python match: statement introduced in Python 3.10 - example: https://github.com/dosisod/refurb/blob/master/refurb/checks/pathlib/read_text.py https://github.com/dosisod/refurb/blob/master/refurb/checks/...
- datalopers 4y agoThat code, starting on line 41, has convinced me to continue avoiding Python. Python has went from a clear concise language with one right way to do things, to outpacing C++ when it comes to adding esoteric language features.
- zem 4y agothat code is pattern matching on an ast node; the complexity is from the data structure involved, not the language. there is no more concise way to match a specific node and get at the relevant fields, just various different forms of verbose code.
- ac130kz 4y agoThat code has nothing to do with conciseness, he could've extracted the arguments in separate variables
- jraph 4y agoIf anything, this matching on an AST is very readable and elegant, and that just convinced me that the syntax of Python seems really good to write a parser.
- sireat 4y agothat AST parsing code looks like something written in Scala ... seems like Scala is moving to more Python like and Python is moving to add some features from Scala.
- Waterluvian 4y agoI’m actually genuinely curious what alternative you might present from any language. I always thought that was relatively concise for such a complex data structure.
- fny 4y agoThey're complaining about types, :=, and match.
- dosisod 4y agoThank you! I have found that pattern matching has made this project a whole lot easier. One of the other cool features about this project is how the check() functions are loaded based off the type of the node argument: https://github.com/dosisod/refurb/blob/master/refurb/loader.py#L55-L63 https://github.com/dosisod/refurb/blob/master/refurb/loader.... This also supports type unions, so you can register your check for binary and unary expressions by typing node as `BinaryExpr | UnaryExpr`.
- mikewarot 4y agoWow, that's a lot of code smell to me. Why specify all the specific fragments of mypy.nodes, instead of import *? Docstrings in the middle of code should just die 1/2 of the screen is wasted Why use a case statement if it only has one match? I can't even figure out what lines 43-55 do, but that's a lot of nesting
- serpent_skis 4y agoThe reason for using a match statement, even though there is only “one” case, is because it’s matching against a complex and (as you mentioned) deeply nested pattern. Lines 43-56 match the current AST node against the pattern (shown here as pseudo-python): with open(_) as f: _ = f.read() (Where _ can be anything, and f can be any name). The traditional alternative to lines 43-56 would be dozens of lines that check the top level of the AST, and if that matches against what the top level of the pattern is expecting, diving one layer deeper and checking that level against the next level of the pattern, etc, until you reach the leaves of your pattern. Once your familiar with pattern matching syntax, this is much easier to quickly read/grok/audit.
- UncleEntity 4y agoSo it can do multi-level tree matching? Very interesting… One of the projects I occasionally poke at is a burs (bottom-up rewrite system) generator used to do cost-based tree rewriting. Think this is a concept that might make sense in that context, just need to find the time to play…
- david2ndaccount 4y ago> Why specify all the specific fragments of mypy.nodes, instead of import *? glob imports are discouraged as it is hard to figure out where a name comes from (or if it’s just a typo). > Docstrings in the middle of code should just die 1/2 of the screen is wasted You mean the class level doc string? That shows up in the help of that class, so there is nowhere else to put it. I also don’t see the point of a match with a single case.
- hexane360 4y ago