Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do we want to talk about type annotations in expressive code? #46

Open
sneakers-the-rat opened this issue Oct 22, 2024 · 2 comments
Open
Labels
enhancement New feature or request

Comments

@sneakers-the-rat
Copy link
Contributor

sneakers-the-rat commented Oct 22, 2024

I can never find the blogpost when i need it but it has been argued that basically untyped and type hinted python are diverging into two separable dialects with different coding norms and practices. I am firmly in the "type annotations all day every day" camp because i love them, and i feel like they are pretty squarely in the core of what one might think of as 'expressive python.'

So thinking about this lesson: https://github.com/pyOpenSci/lessons/blob/main/clean-modular-code/activity-3/clean-code-activity-3.md

Part 2 is about mismatches in argument type, right.

the function here:

def process_published_date(date_parts):

should have a function signature like

def process_published_date(date_parts: list[list[int]]) -> pd.Timestamp: ...

and the value of introducing that idea is that you would indicate to someone who was calling your function that their arguments might be incorrect before they even try to call the function.

Type hints also propagate, so if you had

def inner_function(date_parts: list[list[int]]) -> pd.Timestamp:
    return process_published_date(date_parts)

def outer_function(year: int, month: int, day: int) -> pd.Timestamp:
    return inner_function([[year, month, day]])

def third_function(parts_again: list[list[int]]) -> pd.Timestamp:
    parts = parts_again[0]
    return outer_function(parts[0], parts[1], parts[2])


third_function(['hey', 'sup'])

then you'd be told from a long distance that your arguments are wrong and you can correct the problem without needing to dig deep into a call stack to see what's actually causing the problem. practically, that translates to all the code that needs to use your code being easier to use and less error prone.

I think for the sake of a beginner lesson just showing them the base types int, float, list[T], tuple[T, U, ...] + maybe dataclasses would be enough. I'd be happy to write it.

@willingc
Copy link
Contributor

I think that I would cover #46 and #45 in a more advanced lesson on "expressive code". Though great topics, they are likely too much cognitive load for a person new to Python.

@willingc willingc added the enhancement New feature or request label Oct 23, 2024
@sneakers-the-rat
Copy link
Contributor Author

definitely an "after you've written a function or two" lesson. agreed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants