News for hackers. Stuff that matters.
Re:Why if TYPE_CHECKING? by bruce343434 on Wednesday December 20, 2023 @07:28AM (#38707867)
But it is its own thing that compiles down to javascript, and is otherwise syntactically and semantically "very very similar" to javascript. It IS always statically typechecked and has good, or at least well defined, semantics for typing. Typescript does not change base javascript. It really is a well separated layer on top of javascript.

The way python does it is different. There's no "typethon". The python grammar was simply extended to allow any random expression as a "type".

In fact:

    x: print("hi") = 4
is valid python, and it does print "hi" without even so much as a warning. Personally, I don't think "print(hi)" should be a valid type.
Re:Why if TYPE_CHECKING? by tomn on Wednesday December 20, 2023 @07:59AM (#38708121)
This feels like splitting hairs -- python and typescript are equivalent, as they are both dynamically typed languages with type annotations.

The fact that the typescript implementation does not run typescript directly, and cPython does not type check before running the code, are both implementation details.

Typescript is just a better type system on top of javascript than python's type system is on top of untyped python, because typescript is a cohesive design rather than a collection or random stuff accumulated over time.

There's no reason that couldn't have happened for python, it just didn't.

Re:Why if TYPE_CHECKING? by lijok on Wednesday December 20, 2023 @08:01AM (#38708147)
> Personally, I don't think "print(hi)" should be a valid type.

And it isn't:

  a.py:1: error: Invalid type comment or annotation  [valid-type]
  a.py:1: note: Suggestion: use print[...] instead of print(...)
  Found 1 error in 1 file (checked 1 source file)
Re:Why if TYPE_CHECKING? by n3150n on Wednesday December 20, 2023 @10:15AM (#38709544)
It is not. As the function `print("hi")` is executed, the argument type in this case becomes `Any`, as it would be for any other executed function.

As stated in the typing documentation [0]: "the only legal parameters for type are classes, Any, type variables, and unions of any of these types".

[0] https://docs.python.org/3/library/typing.html

Re:Why if TYPE_CHECKING? by bruce343434 on Wednesday December 20, 2023 @10:43AM (#38709873)
That's fine and all. But it runs without errors or warnings.

I can put that in main.py and do `python3 main.py` and it will simply run fine.

What is the point of this whole system if it's not enforced?

Re:Why if TYPE_CHECKING? by mgkimsal on Thursday December 21, 2023 @01:32PM (#38724619)
Amen.
Re:Why if TYPE_CHECKING? by tomn on Thursday December 21, 2023 @08:17PM (#38729679)
If you want type checking, run mypy.

If you don't want type checking, don't run mypy.

I don't see what's so difficult. If it was technically possible to run programs that have type errors in other languages, they would have the option to ignore type errors too, because it's convenient.

Typescript also ignores errors (generating js output for you to run) by default, so what's the point if it's not enforced?

Re:Why if TYPE_CHECKING? by bruce343434 on Friday December 22, 2023 @06:31AM (#38733192)
Typescript does not ignore type errors by default. Type error = fail to compile
Re:Why if TYPE_CHECKING? by tomn on Friday December 22, 2023 @07:17AM (#38733425)
Try it, it produces output by default. You have to add a flag/option to change this https://www.typescriptlang.org/tsconfig/#noEmitOnError
Re:Why if TYPE_CHECKING? by vbezhenar on Wednesday December 20, 2023 @12:41PM (#38711313)
Typescript "compilation" is just stripping out types, mostly. JavaScript is a subset of TypeScript (mostly). There was even a proposal to extend JavaScript comments syntax, so all typescript types would become comments (so you could just run typescript code with JS interpreter).