Earlier quoted context omitted.
I must be missing something here. Wouldn't an unsigned int be unable to represent -1 since the sign bit is part of the value?
The -1 here is hiding the fact that -1 is really just 0xffffffff due to two's complement (architecture dependant of course) And printing it as %d is technically a misuse of printf, since %d means print as a signed integer. If you did %u (print as unsigned integer) instead you'd see the value is really 4,294,967,295 (again, platform dependant)
Python types have an expectations problem
71–80 of 115 posts
Re: Python types have an expectations problem
#72Earlier quoted context omitted.
Python's type hints are great as machine-checkable statements about constraints on the behavior of your code. It's not strictly true that they're unavailable at runtime. > Don't count on them at runtime here either If you're adventurous enough, you can reflect on the type hints and check things yourself at runtime, but you have to understand that the type hints aren't meant for this and they could well blow up in you…
> It's not strictly true that they're unavailable at runtime. So is it possible for me to enforce type hints at runtime, so my program crashes if a type is ever wrong? How do I turn this checking on?
That said, using these sorts of tools (or trying to enable runtime type checks in general) is usually a misunderstanding of how the type checking works. Think of type annotations less as a way to enforce types, and more as a way to explicitly declare intention. If you annotate a function as taking a string and returning an int, then you're saying that, if the caller obeys their side of the deal and passes you a string, then you'll obey your side of the deal and return an integer. Moreover, a type checker can demonstrate that this is the case: that for every path through your function, assuming the input is always a string, then you will return an integer. This frees you from runtime checks entirely, because the checks can happen entirely at compile time: assuming the input types are correct, your code will return values of the correct type in return. If your whole program is type checked, then we can be confident that the whole program behaves correctly in terms of types.
There are two big exceptions to this. Firstly, runtime data cannot be checked in this way, so needs to be checked at runtime. Usually this happens at very explicit boundaries: you load a TOML file, and use Pydantic to validate the file and parse it into a Python structure. If the Pydantic check succeeded, then you know the data had the correct types, and therefore that your function will behave correctly as outlined above. If it failed, you get a runtime error at the exact point that the invalid data enters your system. The danger is doing something like json.loads and then not validating the result in any way.
The second exception is when you manually choose to ignore the type system and tell it what's going on. This includes type assertions where you override Mypy's expectations for what a type should be, or the use of the `Any` type. In these situations, you deliberately can the compact you've made with the type checker, but you also get a chance to handle cases of extreme dynamism in Python that the type checker can't deal with. These sections of your code will naturally be unsafe, but are usually more explicit and so can be checked more carefully in code review.
The result is that, with a type system that handles this stuff well, you do not need to enforce type hints at runtime (and trying to enforce type hints at runtime is usually a sign of not leaning sufficiently into the type system in the first place). That said, "a type system that handles this stuff well" is the key phrase here - Typescript is a good example of a tool that works well as a static-only type checker, in that it gives you a lot of power to define types that match the underlying runtime. In my experience, there isn't yet a tool powerful enough to handle that in Python, which can make developing with type hints quite frustrating at times. But I don't think runtime type checking would change that at all.
Re: Python types have an expectations problem
#73Earlier quoted context omitted.
To be fair, TypeScript is a bit different because you can use it to compile to JavaScript rather than merely annotating in JavaScript files, which is crucial because it means that compilation can _fail_ to produce JavaScript. If I recall correctly, you can still have it produce JavaScript even if the type checking fails, but the fact that you can't just directly run the TypeScript makes a difference. With the Python…
I'm not sure that's really a practical distinction between Typescript and Python. Most of the practical bundling setups I've seen use a tool like Babel or Esbuild, which doesn't do any type checking - instead the type checking is done as a linting/testing check beforehand, the same as with Python. And as you point out, even if you compile directly with Typescript, it will quite happily compile code that doesn't pass…
I think the difference is really one of UX (or DX I guess) and availability: Yes, if you already work with a toolchain, there won't be a difference. However, lots of people don't. If you just use the language runtime on its own and want to get your program running with the minimum possible effort, then for typescript, you'll run the transpiler and feed the resulting JS to a browser or nodejs vm; for python, you'll just run your program with the python interpreter directly.
The thing is that for the "minimum" typescript workflow, type checking is per default performed, while for the minimum python workflow it isn't. That you could also disable type checking for typescript or use a linter to get it done in python is besides the point - those are additional options and tools that you have to spend additional effort to activate - and you have to know about them in the first place. Someone who just has some basic skills in the language is unlikely to do so, so effectively, type checking for them is performed in typescript but not in python.
Re: Python types have an expectations problem
#74Earlier quoted context omitted.
It should be fair obvious that when you use a very verbose very boiler plate heavy coding style like static typing that because the programmer is writing a lot more lines of code per software feature and because the programmers make errors on a per line written basis that the number of the bugs will drastically increase. You can easily find information on duck typed programs being significantly shorter and that stati…
I have written type-annotated Python for as long as type annotations have been a thing. "Number of bugs will drastically increase" is simply wrong. Type checking prevents bugs before they ship.
Type checking mostly shows that code can be compiled successfully. The thing it was designed to do.
Re: Python types have an expectations problem
#75Earlier quoted context omitted.
It should be fair obvious that when you use a very verbose very boiler plate heavy coding style like static typing that because the programmer is writing a lot more lines of code per software feature and because the programmers make errors on a per line written basis that the number of the bugs will drastically increase. You can easily find information on duck typed programs being significantly shorter and that stati…
So not only did you pull the original comment's assertions out of your _bag of holding_ but rather than citing sources when asked, you doubled down on "well if you know you know" and claim that the (often relatively small) increases to line lengths of type-annotated programs somehow magically increases the number of bugs, again with no evidence. If you're not going to bring bold numbers to support bold claims, why sh…
I mean you could always try to find some sources to try and prove me wrong if you want?
Re: Python types have an expectations problem
#76Re: Python types have an expectations problem
#77Earlier quoted context omitted.
I have written type-annotated Python for as long as type annotations have been a thing. "Number of bugs will drastically increase" is simply wrong. Type checking prevents bugs before they ship.
There are numerous academic studies that show that type checking does not reduce errors in shipped code by an statistically significant amount. Type checking mostly shows that code can be compiled successfully. The thing it was designed to do.
i've read some of these studies, they don't prove much. it's typically languages with inexpressive type systems like java.
>Type checking mostly shows that code can be compiled successfully. The thing it was designed to do.
what languages are you thinking of when you say things like this? these debates are useless because one person is thinking of the type system of language X and the other person is thinking of language Y. a lot of heat but little light.
Re: Python types have an expectations problem
#78Earlier quoted context omitted.
So not only did you pull the original comment's assertions out of your _bag of holding_ but rather than citing sources when asked, you doubled down on "well if you know you know" and claim that the (often relatively small) increases to line lengths of type-annotated programs somehow magically increases the number of bugs, again with no evidence. If you're not going to bring bold numbers to support bold claims, why sh…
There is nothing bold about the claims, knowing the differences between the various typing systems and their advantages and disadvantages is standard stuff. I mean you could always try to find some sources to try and prove me wrong if you want?
"I mean you could always try to find some sources to try and prove me wrong if you want? "
That's not really how it works though. If you make a claim, either you back-it up or it's irrelevant.
Case and point: I believe that a flying tea pot created the universe. By your logic I am right and it should be up to you to prove me wrong.
So to conclude this argument, if you are correct, you should prove it as the person who responded to you asked you to do.
Failing that,your comment goes back to being nothing more than your opinion instead of a fact.
Re: Python types have an expectations problem
#79In Python it's common to have functions whose return types depend on the run-time arguments. For example: def fooify(x): if isinstance(x, list): map(fooify, x) else: x * 2 I guess this is to make scripting more forgiving. In typical typed languages, you would have two functions instead: fooify : number -> number fooifyMany : [number] -> [number] But in the Python community, it's common to have a big function with man…
In practice, this would be solved with `typing.overload`[0]. Using you example: from typing import overload @overload def fooify(x: int) -> int: ... @overload def fooify(x: list[int]) -> list[int]: ... def fooify(x: list[int] | int) -> list[int] | int: if isinstance(x, list): return [fooify(_x) for _x in x] return x * 2 [0] https://docs.python.org/3/library/typing.html#overload
Meanwhile it's not even possible to express such things in other static type systems. So I'm not exactly an unhappy customer, but it does put certain things tantalizingly close, but still out of reach without a ton of clunky boilerplate and LoC explosion.
Re: Python types have an expectations problem
#80Earlier quoted context omitted.
You can do that as a docstring inside the function. There are even some basic rules like... def extract_coordiates( unreseted_idx_from_df, json_data: dict, notna_idxs: list, na_idxs: list ) -> list: """ Extracting coordinates from the json_data['data'] and returning the 4 positions of those as an array Parameters ---------- json_data: json Data extracted from tabula.read_pdf and using output_format='json' notna_idxs:…
I think you misinterpreted parent comment. My reading was that they would have preferred type-hints to be defined as part of docstring style comments instead of having the type hints inline with the code. From tooling point of view it doesn't make any difference, both forms should be functionally equivalent.