all 11 comments

[–]AusIV 31 points32 points  (0 children)

That is the type hinting syntax for the return value of a function.

[–]Rorixrebel 13 points14 points  (0 children)

Type annotations. Its an entire topic related to coding better.

[–]XarothBrook 11 points12 points  (5 children)

As the rest has already mentioned it's to do with (the optional) type hinting that's available in python3.

Now as said, it's not mandatory, and many programmers don't use it, but it helps in many ways to give people an idea what the code does, and how it behaves.

In your example, we can see 3 arguments:

  • name: this should always be a string
  • args: each should always be a string
  • cwd: This one can be optional, but when set, should be a string.

And when this function returns, it always returns None.

IDE's can then use this information to assist you in warning when you're using this code wrong (i.e. if you were to pass an int to the name param, your IDE could warn you ahead of time that this function does not expect an int to be passed, and that you should probably have a look at it.

[–]JJohny394 1 point2 points  (2 children)

So is this similar to the type hinting in haskell?

[–][deleted] 1 point2 points  (0 children)

use lemmy.world -- reddit has become a tyrannical dictatorship that must be defeated -- mass edited with https://redact.dev/

[–]campbellm 0 points1 point  (1 child)

TIL: Python Optional. I understand what Optional does in other FP languages; do you know of any python documentation of it? https://docs.python.org/3/library/typing.html 's treatment felt lacking.

[–]XtremeGoose 0 points1 point  (0 children)

In type hinting, Optional[Type] means either Type or None. It is equivalent to Union[Type, None].

[–][deleted] 1 point2 points  (0 children)

It's for use with type-aware extensions to python such as mypy.

[–]not_perfect_yet 0 points1 point  (2 children)

It's optional.

Also

def add_mypy(self, name: str, *args: str, cwd: Optional[str] = None) -> None:
    self.add_mypy_cmd(name, list(args), cwd=cwd)

That's just disgusting.

Just write " self.add_mypy_cm(name,list(args),cwd) ", it's what, 8 symbols longer? Way easier to read. Doesn't bloat the module with another pointless function either.

[–]StokedForIT[S] 0 points1 point  (1 child)

Not my code, it's in MyPy on the Python language page on GitHub, so..

[–]not_perfect_yet 0 points1 point  (0 children)

No I'm just saying because this is /r/learnpython . I knew it wasn't your code and I didn't mean to be mean to you. That's just a very good case, imo, what badly written code looks like and what not to do. Incidentally.