MLBZ521 / MacAdmin

A collection of scripts and tools for managing Apple Devices
MIT License
166 stars 33 forks source link

Python type hinting #17

Closed macjustice closed 1 year ago

macjustice commented 2 years ago

I was checking out your bomgar installer scripts, which I'm extremely thankful to run across since I just spent the better part of the day reinventing this wheel badly.

As of Python 3.5, you can use type hinting to define what types a function's parameters can accept, as well as define what type a function will return.

I noticed you doing some manual input validation in Install-BomgarJumpClient.py and thought you could save yourself some time by changing the function definition from def execute_process(command, input=None):

to def execute_process(command: str, input: str = None):

That would validate that anything passed to command or input be a String, and you can drop the manual input validation.

Anyhow, thanks for the awesome work, I'm giving the script a try now!

MLBZ521 commented 2 years ago

Hey @macjustice.

Glad my scripts are useful to you!

I've only recently (last year or so) started seeing that used and have only really been employing it in my PkgBot project as the underlying framework it's written on does perform the validation.

That said, I do not think Python automatically validates variable types. Per the typing docs:

The Python runtime does not enforce function and variable type annotations. They can be used by third party tools such as type checkers, IDEs, linters, etc.

And testing confirms:


>>> def greeting(name: str) -> str:
...     return f'Hello {name}'
... 
>>> greeting("Zack")
'Hello Zack'
>>> greeting("1")
'Hello 1'
>>> greeting(1)
'Hello 1'
macjustice commented 2 years ago

Shoot, you're correct. I was getting used to the type safety in Swift so when I got into Python's type hinting I overestimated it. Useful for IDEs and CI contexts, stuff like that, but clearly not really a good guardrail for an admin manually running the code. Nevermind! 😅

On Wed, Aug 31, 2022 at 11:49 PM Zack T @.***> wrote:

Hey @macjustice https://github.com/macjustice.

Glad my scripts are useful to you!

I've only recently (last year or so) started seeing that used and have only really been employing it in my PkgBot project as the underlying framework it's written on does perform the validation.

That said, I do not think Python automatically validates variable types. Per the typing docs https://docs.python.org/3/library/typing.html:

The Python runtime does not enforce function and variable type annotations. They can be used by third party tools such as type checkers, IDEs, linters, etc.

And testing confirms:

def greeting(name: str) -> str: ... return f'Hello {name}' ... >>> greeting("Zack")'Hello Zack'>>> greeting("1")'Hello 1'>>> greeting(1)'Hello 1'

— Reply to this email directly, view it on GitHub https://github.com/MLBZ521/MacAdmin/issues/17#issuecomment-1233822351, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEDGS5JAVJD5A5LELW3PS3V4BGZNANCNFSM6AAAAAAQB2VSTE . You are receiving this because you were mentioned.Message ID: @.***>

MLBZ521 commented 1 year ago

No worries! While Python doesn't do it natively, libraries like Pydantic do offer this which you may be interested in.

I'll go ahead and close this issue for now, but if you want to continue the conversation, feel free.