TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Python's Type Checking Renaissance

150 点作者 davidfstr超过 4 年前

18 条评论

nynx超过 4 年前
People who use dynamically-typed languages are finally realizing that having to simulate a type-checker mentally is a bad idea.<p>Also, many statically-typed languages do not take advantage of the flexibility a good type-system can provide. For example, C, C++, Java.
评论 #25918103 未加载
评论 #25925455 未加载
评论 #25925383 未加载
评论 #25918480 未加载
评论 #25926682 未加载
评论 #25925185 未加载
评论 #25918037 未加载
评论 #25932042 未加载
评论 #25925315 未加载
评论 #25918083 未加载
评论 #25923627 未加载
评论 #25917494 未加载
评论 #25917580 未加载
avolcano超过 4 年前
I&#x27;m excited about Python&#x27;s typing potential. I recently rewrote an API from TypeScript to Kotlin since I am fairly unhappy with the server-side TS ecosystem, but ran across <a href="https:&#x2F;&#x2F;github.com&#x2F;tiangolo&#x2F;fastapi" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;tiangolo&#x2F;fastapi</a> when exploring options and really dig it - seems to be by _far_ the lowest-ceremony way to make an HTTP API with static types that integrate with parsing&#x2F;validation (something TypeScript is still really bad at, unless you bring your own runtime typing libraries...).
评论 #25917708 未加载
评论 #25922348 未加载
brundolf超过 4 年前
I sure hope things continue to progress, but right now, having come from TypeScript, Python is quite a ways behind when it comes to static types. A few major differences:<p>- Library support for static types is not very good. This can be fixed, of course, but it&#x27;s also very hard to fix in a concerted way. It&#x27;ll just depend on the community getting on board.<p>- The syntax is limited. There isn&#x27;t proper support for declaring generics, you have to declare a separate TypeVar, as a Python variable, somewhere else in scope and it just... gets used to approximate a generic. It mostly works, but sometimes it doesn&#x27;t, and it&#x27;s very unintuitive and awkward. And then concepts like Callable, Union, TypedDict, and Optional don&#x27;t have dedicated syntax for readability; they&#x27;re generic types that you have to import and parameterize. Etc.<p>- Support isn&#x27;t great for highly &quot;dynamic&quot; data. TypeScript gives you powerful features for reasoning about dynamic property-sets of objects (dicts), combining and separating them, duck-typing, doing really complex inference, etc. These features in Python are usually some combination of unreliable, third-party, syntactically awkward, and so on.<p>- Inconsistency between different type-checkers. You&#x27;d think the fact that Python has standardized type syntax would help with consistency, but what it actually means is that everyone gets to define their own semantics for the same syntax. Different checkers mostly orbit around the same semantics, but there are always gaps. So for example, MyPy does a pretty good job of being strict and smart, but it&#x27;s really slow. So you&#x27;ll end up using an IDE-optimized checker for development, like Pytorch, but Pytorch will allow some things that MyPy doesn&#x27;t and not allow some things that MyPy does. So you can use your IDE to get <i>most</i> of the way there, but you always have to remember to run a &quot;real&quot; type-check before you commit, or you may break the build in CI.<p>I should point out the one big advantage that Python has here: unlike TypeScript you don&#x27;t need a build step, because Python interpreters can parse (and throw away) the type annotations. That&#x27;s pretty nice, especially for gradual adoption&#x2F;casual typing of scripts.<p>All of the problems (except maybe the syntax) are solvable, and I genuinely hope they get solved. For now, if you stick to primitives and core or class-based data structures you&#x27;ll have a great experience with Python types. If you do anything more complex, the results will be mixed. This is of course much better than nothing, but it could be a lot better still. If you&#x27;re picking between typed Python and TypeScript for a new project, it&#x27;s worth factoring in.
评论 #25918249 未加载
评论 #25918116 未加载
评论 #25917412 未加载
mpweiher超过 4 年前
Hmm...the basic development cycle described is horribly broken:<p><pre><code> Write a bunch of new code. Repeat 4-6 times, in rapid succession: Run program to manually test. Find basic error (like a missing import). Fix basic error. Debug&#x2F;fix deeper errors in the new code </code></pre> With this, just about anything will help, including static typing.<p>The problem is the following line<p><pre><code> Run program to manually test. </code></pre> You should avoid manual testing at (almost) all cost. Write an automated test instead. It is effort your are expending anyway, why throw it away?? And while you&#x27;re there, write the test beforehand, to clarify the specification of the code you&#x27;re trying to write.<p>With such a TDD cycle, those problems wouldn&#x27;t have happened.<p><pre><code> Repeat Write a test Make it pass [Commit] Refactor Commit </code></pre> Mind you, static typing can still help in this scenario, but <i>not</i> in getting the code to work and helping it remain functional. It helps as verified documentation that eases understanding when reading the code.
评论 #26032594 未加载
booleandilemma超过 4 年前
I&#x27;m glad that strong typing is getting popular and it&#x27;s not just for weak minds anymore.<p>But (for new projects anyway) what&#x27;s the point of using Python with types instead of using a real typed language like Java or C#?
评论 #25918799 未加载
评论 #25923891 未加载
评论 #25919394 未加载
评论 #25918461 未加载
评论 #25918495 未加载
Majestic121超过 4 年前
&#x27;Renaissance&#x27; implies a first version, was there any attempt to add typing in older versions of python that were dropped ?<p>As a side note, for a related project linked to typinglike the TypedDict, I found that Pydantic is pretty amazing.
评论 #25917194 未加载
评论 #25924278 未加载
评论 #25917191 未加载
评论 #25926205 未加载
MR4D超过 4 年前
Given that Python type checkers are becoming all the rage, will we ever see a compiler that actually uses those types to increase performance?
评论 #25924528 未加载
评论 #25925806 未加载
stereo超过 4 年前
For those who have never used type checking in Python, what&#x27;s a good introduction?
评论 #25917537 未加载
评论 #25918185 未加载
评论 #25917530 未加载
评论 #25921075 未加载
评论 #25924962 未加载
parham超过 4 年前
Why would you add type checking to Python! Using another language with static typing would be the solution. It’s not like the browser where you have to use Javascript so you add another layer to your toolchain.
评论 #25925268 未加载
评论 #25925623 未加载
评论 #25925391 未加载
cjdell超过 4 年前
When proponents of dynamic languages tell me &quot;that&#x27;s what the unit tests are for&quot;, I say good for you that you have unit tests.<p>However a unit test may simply inform you that you have something wrong with your program, whereas a type checker will (more often that not) show you exactly where the error is, and usually as you were typing it.<p>Reality is that unit tests and type checking both have their place even if there is overlap between the two. Type checkers can however remove the need for trivial and repetitive unit tests.
评论 #25917671 未加载
评论 #25924337 未加载
评论 #25923099 未加载
anamax超过 4 年前
As is typical in these discussions, folks are confusing representation with kind.<p>&quot;type&quot; systems help with representation errors, but those tend to be easy to find without static typing.<p>One problem with static typing is that I&#x27;m forced to address all of the representation issues before I can do any testing. That &quot;premature optimization&quot; is hell on incremental development.<p>Another problem is that static typing tends to encourage languages which are designed around making type checking easy. That impacts program design.
评论 #25933545 未加载
Decabytes超过 4 年前
I really like being able to add types in Python. But I wish it was more integrated into cPython, so I didn&#x27;t have to call mypy on my file. I admit it&#x27;s been awhile so maybe it&#x27;s changed, but I like how in Racket I can just add types, change my language to #lang typed&#x2F;racket and then boom we are type checking.
currymj超过 4 年前
is there a high quality introduction to Python type checking for people who already know Python?<p>maybe something very opinionated about how you&#x27;re supposed to use the features, sort of a &quot;Hitchhiker&#x27;s Guide to Type Checking&quot;.<p>if I Google around I find either documentation or fairly low quality blog posts.
评论 #26032691 未加载
评论 #25923901 未加载
评论 #25924950 未加载
zappo2938超过 4 年前
I recently started working through python tutorials in videos, books, and blogs on statistics and machine learning. As a front end developer who has over the last several years started to adhere to using immutable objects and functional programming in JavaScript and subsequently TypeScript, which doesn&#x27;t matter as much from my point of view. All the bugs that arise in the python tutorials from data mutation and side effects in class methods (looking at you inplace=True in Pandas) is what disturbs me the most about my first exposure to Python.<p>I really hope to start seeing some immutable architecture and structuring in Python more than type checking, as my first impression.
antipaul超过 4 年前
What’s the big deal about type checking?<p>I haven’t worked on large code bases, only run small ML analyses on tabular data. Can someone be concrete with the advantages and use cases?
评论 #25917238 未加载
评论 #25917255 未加载
评论 #25917378 未加载
评论 #25923546 未加载
评论 #25917524 未加载
评论 #25918184 未加载
评论 #25917645 未加载
评论 #25917288 未加载
评论 #25919986 未加载
Geminidog超过 4 年前
With pycharm you don&#x27;t even have to run the code. Your type checker will highlight all errors real time.
toolslive超过 4 年前
<a href="https:&#x2F;&#x2F;i.redd.it&#x2F;umtx1gc28eb41.jpg" rel="nofollow">https:&#x2F;&#x2F;i.redd.it&#x2F;umtx1gc28eb41.jpg</a>
评论 #25918806 未加载
Galanwe超过 4 年前
I feel like type checking for Python is a plot by engineers bored by a language that is too pragmatic and efficient for their taste.
评论 #25917346 未加载
评论 #25917246 未加载
评论 #25917622 未加载
评论 #25917216 未加载