classMyClass:
def__init__(self, x: int):
self.whatever: int = x
deffoo(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()
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.
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.
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.
This is literally a getter function. How is s getter awful code? It’s the simplest function there is. The only function simpler than that is returning the input itself.
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?
classMyClass:
def__init__(self, value):
self._whatever = value
@propertydefwhatever(self):
returnself._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.
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?
def foo(x): return x.whatevr
No linter is going to catch that.
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 offoo()
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.
Yes because you used static type annotations. This thread was about code that doesn’t use static types (or static type annotations/hints).
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.
Python doesn’t check the types of function headers though. They’re only hints for the programmer.
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.
Linters 100% won’t. A static type checker is not a linter.
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.
Autocorrect got you pretty bad, there.
I was very confused, why we’re suddenly talking about rationing food during winter. 🙃
Yes you can use static type hinting and the static type checker (Mypy or Pyright) will catch that. Linters (Pylint) won’t.
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.
Always love seeing the trope:
This is literally a getter function. How is s getter awful code? It’s the simplest function there is. The only function simpler than that is returning the input itself.
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?
It’s a one line function in an example. It’s a getter.
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…
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.
I use a spell checker in my IDE. It would catch this.
What’s awful about this example? The only thing I do is access an object member. Does your code not do that??
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.
Lmao, and they say dynamic typing is supposed to speed up the developer.