• UndercoverUlrikHD@programming.dev
    link
    fedilink
    arrow-up
    0
    ·
    3 days 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()

    • FizzyOrange@programming.dev
      link
      fedilink
      arrow-up
      0
      ·
      3 days ago

      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
        link
        fedilink
        arrow-up
        0
        ·
        2 days 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.

        • FizzyOrange@programming.dev
          link
          fedilink
          arrow-up
          0
          ·
          1 day ago

          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
            link
            fedilink
            arrow-up
            0
            ·
            1 day ago

            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
              link
              fedilink
              arrow-up
              0
              ·
              1 day 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!

      • UndercoverUlrikHD@programming.dev
        link
        fedilink
        arrow-up
        0
        ·
        3 days 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.

          • UndercoverUlrikHD@programming.dev
            link
            fedilink
            arrow-up
            0
            ·
            edit-2
            2 days 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.

    • Starbuncle@lemmy.ca
      link
      fedilink
      English
      arrow-up
      0
      ·
      3 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.