814
submitted 1 day ago by [email protected] to c/[email protected]
you are viewing a single comment's thread
view the rest of the comments
[-] [email protected] 32 points 1 day ago

Static types are great, but not exactly what would have helped here, any decent language or at least a linter should catch the use of a not declared identifier.

[-] [email protected] 17 points 1 day ago
def foo(x):
  return x.whatevr

No linter is going to catch that.

[-] [email protected] 13 points 1 day ago
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()

[-] [email protected] 4 points 1 day ago

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

[-] [email protected] 1 points 13 hours ago

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.

[-] [email protected] 6 points 1 day 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.

[-] [email protected] 1 points 1 day ago

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

[-] [email protected] 1 points 1 day ago

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.

[-] [email protected] 1 points 14 hours ago

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

[-] [email protected] 1 points 13 hours ago* (last edited 13 hours ago)

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.

[-] [email protected] 1 points 13 hours 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.

[-] [email protected] 7 points 1 day ago

It's python, just use type hinting already and your linter will catch that.

Also some winters can look at the use of food and see the type being passed in.

[-] [email protected] 14 points 1 day ago

Autocorrect got you pretty bad, there.

I was very confused, why we're suddenly talking about rationing food during winter. ๐Ÿ™ƒ

[-] [email protected] 4 points 1 day ago

Holy crap that's wild, new phones autocorrect is out to get me

[-] [email protected] 6 points 1 day ago

Yes you can use static type hinting and the static type checker (Mypy or Pyright) will catch that. Linters (Pylint) won't.

[-] [email protected] 0 points 1 day ago

Not with an example that simple and poor, no.

If you have done the minimum and at least set a type hint, or if your ide is smart enough to check what calls the function and what it passes, then it'll be flagged.

[-] [email protected] -3 points 1 day ago

Always love seeing the trope:

*writes awful code*

See! This is why this language sucks!

[-] [email protected] 3 points 1 day ago* (last edited 15 hours ago)

This is literally a getter function. How is a getter awful code? It's the simplest function there is. The only function simpler than that is returning the input itself.

[-] [email protected] 0 points 19 hours ago

How does "foo" mean "get"? Half the battle of writing correct code is writing code that's easy to interpret. Do you always look at the guts of every function you're about to use?

[-] [email protected] 2 points 15 hours ago

It's a one line function in an example. It's a getter.

[-] [email protected] 4 points 1 day ago

How would you make it non-awful, without specifying static types?

I guess, a unit test would catch it, but needing 100% test coverage to catch typos isn't exactly great...

[-] [email protected] 1 points 9 hours ago* (last edited 9 hours ago)

I would do

class MyClass:
    def __init__(self, value):
        self._whatever = value

    @property
    def whatever(self):
        return self._whatever

Personally, I would type hint all of that but I'm just showing how you can do it without types. Your linter should be smart enough to say "hey dumbass did you mean this other thing"? Also since we didn't create a setter you can't arbitrarily overwrite the value of whatever so thats neat.

And I'll just say before I post that I'm on mobile and I'm sorry if the formatting is fucked. I'm not going to fix it.

[-] [email protected] 0 points 1 day ago

I use a spell checker in my IDE. It would catch this.

[-] [email protected] 3 points 1 day ago

What's awful about this example? The only thing I do is access an object member. Does your code not do that??

[-] [email protected] 0 points 1 day ago* (last edited 1 day ago)

What's the purpose of foo? Why an ambiguous single character variable? What if the property was there but the value was null? Why not use (assuming JS) optional chaining?

I'd approach it more like this:

function getWhatevrProp(userData) (
  const default = { whatevr: "n/a" };

  return { ...default, ...userData }.whatevr;
}

Sorry, read too fast the first time. It's more likely Python. I also don't know Python well enough to give recommendations on that.

[-] [email protected] 3 points 1 day ago

Lmao, and they say dynamic typing is supposed to speed up the developer.

[-] [email protected] 3 points 1 day ago

It's an example to demonstrate that linters cannot reliably detect variable name typos - you need static types. None of the stuff you mentioned is relevant.

The typo in your example is also undetectable by linters. I think you're missing the point.

this post was submitted on 25 Sep 2024
814 points (97.3% liked)

Programming

17071 readers
414 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 [email protected]



founded 1 year ago
MODERATORS