this post was submitted on 25 Sep 2024
889 points (97.6% liked)

Programming

17077 readers
801 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 1 year ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] UndercoverUlrikHD@programming.dev 16 points 2 days ago (3 children)
class MyClass:
    def __init__(self, x: int):
        self.whatever: int = x

def foo(x: MyClass) -> int:
    return x.whatevr

Any decent IDE would give you an error for unresolved attribute. Likewise it would warn you of type error if the type of x.whatever didn't match the return type of foo()

[–] Starbuncle@lemmy.ca 8 points 2 days ago

You're both right. It's possible to write code that gets linted well in Python, yes, but you're often not working with just your code. If a library doesn't use typing properly, not a lot to be done without a ton more effort.

[–] FizzyOrange@programming.dev 5 points 2 days ago (1 children)

Yes because you used static type annotations. This thread was about code that doesn't use static types (or static type annotations/hints).

[–] Eiri@lemmy.ca 1 points 1 day ago (1 children)

Nope, don't need to. WebStorm can even detect nonexistent attributes for objects whose format the back-end decides, and tbh I'm not sure what sort of sorcery it uses.

[–] FizzyOrange@programming.dev 1 points 23 hours ago (1 children)

Yeah IntelliJ does amazingly without type annotations but even it can't do everything. E.g. if you're using libraries without type annotations, or if you don't call functions with every possible type (is your testing that good? No.)

Static types have other benefits anyway so you should use them even if everyone in your team uses IntelliJ.

[–] Eiri@lemmy.ca 1 points 23 hours ago (1 children)

Yeah, our company has been meaning to transition to them for a while. I started saying Jsdoc comments but people complained about the pollution. Then I said fine, we'll do TypeScript one day instead.

That one day has yet to come. I don't actually get to decide much at this company, after all. Aah, technical debt.

[–] FizzyOrange@programming.dev 2 points 23 hours ago

I've never done it but apparently you can actually gradually transition to Typescript one file at a time by renaming them from .js to .ts. Might help a bit. Good luck anyway!

[–] calcopiritus@lemmy.world 1 points 2 days ago (1 children)

Python doesn't check the types of function headers though. They're only hints for the programmer.

[–] UndercoverUlrikHD@programming.dev 2 points 2 days ago (1 children)

OP suggested that linters for python won't catch attribute errors, which they 100% will if you use type hints, as you should.

What happens at runtime is really relevant in this case.

[–] FizzyOrange@programming.dev 1 points 1 day ago (1 children)

Linters 100% won't. A static type checker is not a linter.

[–] UndercoverUlrikHD@programming.dev 1 points 1 day ago* (last edited 1 day ago) (1 children)

I don't want to get into an Internet argument over pedantry. Linter is often used as a catch-all term for static analysis tools.

Wikipedia defines it as

Lint is the computer science term for a static code analysis tool used to flag programming errors, bugs, stylistic errors and suspicious constructs.

Catching type errors and attribute errors would fit under this description, if you use a different, more precise definition at your workplace, cool, then we just have different definitions for it. The point is that your IDE should automatically detect the errors regardless of what you call it.

[–] FizzyOrange@programming.dev 1 points 1 day ago

In common usage a linter detects code that is legal but likely a mistake, or code that doesn't follow best practice.

Although static type checkers do fit in that definition, that definition is overly broad and they would not be called a "linter".

Here is how static type checkers describe themselves:

Pyright is a full-featured, standards-based static type checker for Python.

Mypy is a static type checker for Python.

TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale.

Sorbet is a fast, powerful type checker designed for Ruby.

Here is how linters describe themselves:

Pylint is a static code analyser for Python 2 or 3. ... Pylint analyses your code without actually running it. It checks for errors, enforces a coding standard, looks for code smells, and can make suggestions about how the code could be refactored.

(Ok I guess it's a bit redundant for Pylint to say it is a linter.)

Eslint: The pluggable linting utility for JavaScript and JSX

Clippy: A collection of lints to catch common mistakes and improve your Rust code.

Ruff: An extremely fast Python linter and code formatter, written in Rust.

You get the idea... Linters are heuristic and advisory. Quite different to static type checking.