I mean technically it's a feature you're using, just unintentionally. Kinda like setting a mutable object as a default argument. But I'll admit I got bit by exactly this recently, wrote up an "assignment" using a colon instead of an equal sign and it didn't do anything, but didn't fail either. Somewhat frustratingly, pycharm didn't warn about it either. I should probably check their bug tracker to see if somebody alr…
Python: Even a Feature That You Do Not Use Can Bite You
11–20 of 48 posts
Re: Python: Even a Feature That You Do Not Use Can Bite You
#12Regarding type hints' ability to be arbitrary code, I suspect this is to allow you to use nested type hints from the `typing` library, like `Tuple[int, int]`, as well as more complex or user-defined types. Plus it just gives more flexibility in general.
I'd be interested to hear the author's suggestions on alternative syntax or ways to catch when the programmer intended something other than a type hint, or even arguments that type hints are extraneous and should not have been added - personally, I've found them quite useful recently, in combination with pylint.
Re: Python: Even a Feature That You Do Not Use Can Bite You
#13PS: wait until he reads about := assignement syntax
Re: Python: Even a Feature That You Do Not Use Can Bite You
#14This is true even in languages that don't undergo syntactic extension. If you call a nonexistent function today, your program bombs. Tomorrow, that function could be added, so now something unexpected happens, perhaps.
Lisps are susceptible. Today (foo bar) is diagnosed; tomorrow it's new syntax (not simply a new function), because foo exists as a macro operator.
However, functions and macros, being named, can be namespaced to curtail such issues. Random, ad hoc read syntax extensions, not so much.
One approach is that files, or sections of files, can support annotations which indicate what version of the syntax is required.
Under GCC, if you want only C89, so that any syntactic extensions from later dialects are invalid syntax, you use '-ansi' on the command line to select that dialect. For oinstance, support for // comments disappears, and so x //blah/ y is x / y, rather than x followed by a comment-to-end-of-line.
Re: Python: Even a Feature That You Do Not Use Can Bite You
#15> error: Invalid type: try using Literal[42] instead?
If you don't use the analyzer, the problem is you.
Re: Python: Even a Feature That You Do Not Use Can Bite You
#16This is true. I don't think Python is to blame for it, though. Any language has potential for making a mistake that's syntactically valid but doesn't have the intended meaning. It's like when a former manager of mine typed "call me at your continence" when he meant to say "call me at your convenience".
Re: Python: Even a Feature That You Do Not Use Can Bite You
#17The level of assumed ignorance of the reader in this article is a bit high given the subject matter.
Re: Python: Even a Feature That You Do Not Use Can Bite You
#18It's really just a syntax change. I probably wouldn't have chosen that particular syntax using colons, or I would have forced another keyword like "set" in front of it, but it is what it is. He created a bug and he likely won't do it again, so problem solved.
Re: Python: Even a Feature That You Do Not Use Can Bite You
#19This is an idiotic article because a static analyzer like mypy will find this in a hurry: > error: Invalid type: try using Literal[42] instead? If you don't use the analyzer, the problem is you.
Re: Python: Even a Feature That You Do Not Use Can Bite You
#20I mean technically it's a feature you're using, just unintentionally. Kinda like setting a mutable object as a default argument. But I'll admit I got bit by exactly this recently, wrote up an "assignment" using a colon instead of an equal sign and it didn't do anything, but didn't fail either. Somewhat frustratingly, pycharm didn't warn about it either. I should probably check their bug tracker to see if somebody alr…
I don't think so, because 42 is not a type in Python (it could be one day, though - in TypeScript, 42 can be used as a type, basically an enum with only one value). I think (naively) that it should throw an error.