How working with AWS open-source tools made me a better developer
Tags: opensource, programming, python, cdk
Published: 2023-04-10 14:14:48

## In the beginning

In the beginning God created the heaven and the earth. The earth began to cool, the autotrophs began to drool, Neanderthals developed tools and to-make-a-long-story-short I started learning Python. Much like our early ancestors [benefited from using fire to prepare food](https://www.goodreads.com/book/show/11148989-catching-fire), I benefited from using (and sporadically contributing to) AWS open-source tools.

This is a story of how I went from a self taught Python developer with profound dislike of type hints to tolerating and even using them. All it took to convince me is a multi-billion dollar company with tens of thousands of developers.

## I am not stubborn and I'm not a duck

Contrary to what the intro paragraph led you to believe, I am not a stubborn person. I'd say I'm opinionated, and my opinion about using data types was formed in my teen years. I was a rebel without a pause. It was the time of Python 2.6, which of course has multiple data types, but they are dynamic. This means that you don't have to declare a type for a variable -- the type is inferred by the interpreter at runtime.

You could just re-declare a variable with a different type (`int` to a `float`) and that was fine.
```python
salary = 12000
salary = 12000.0
```

I was amazed. It was *so* easy. No `static final void warranty<List>` that college professors tried to teach me. You just sit down (sitting is optional), type Python and watch magic happen in front of you. An ocassional `TypeError` wasn't gonna stop me. Trying to iterate over an integer? Been there, done that. Neither of those convinced me to spend some time and learn about type hints in Python.

{% embed https://twitter.com/ramalhoorg/status/1638201600995934210?t=IpUCnuxQ2wGuLiY01KOh9Q&s=19 %}

I won't blame you if by reading this you assume I'm a bad developer. I never said I was good :)

## Don't believe the hype

Python and me grew older together and with Python version 3.5 came [type hints](https://peps.python.org/pep-0484/). As their name suggests, they are just hints; Python is still a dynamically typed language. They are not evaluated and they don't affect your application at runtime.

Those hints do help though; modern IDEs understand them and suggest better auto-complete results. They also warn when incorrect types are used:

```python
def add(a, b) -> int:
    return str(a+b)
```

I was aware of these Python advancements but I wasn't aboard the hype train. Who is this Guido person and why is he bringing the noise? Python's syntax was so clean before type hints. My colleagues, who were wiser than me, did get on the hype train and soon I was looking at code that resembled this:

```python
def calculate_avg(salaries: List[int]) -> AverageSalary:
    # perform calculations

salaries: List[int] = [12000, 13000, ...]
avg_salary: AverageSalary = calculate_avg(salaries)
```

It was _awful_. It was noisy, it was unreadable, it was sacrilegious.

## The big melt

Some time ago I was introduced to [AWS CDK for Python](https://docs.aws.amazon.com/cdk/v2/guide/work-with-cdk-python.html) and it was a breath of fresh air after years of Terraform (I still <3 Terraform, don't get me wrong). Then I started noticing that CDK code is strongly typed in all its language variants, and for very good reasons. I liked CDK but did I like it enough?

I gulped, bit the bullet and added types where I had to. I also had a brief adventure with CDK for Typescript and...and... I started **liking** strict typing. It made the code strict, and well defined, and you knew what to expect as a return value. It all made **sense**! Not to mention how it [improves collaboration, makes large code-bases more maintainable and prevents silly bugs](https://instagram-engineering.com/static-analysis-at-scale-an-instagram-story-8f498ab71a0c).

Peasants put down their pitchforks because the [giant's heart has melted](https://artpassions.net/wilde/selfish_giant.html).


## From "I hate this" to...

I truly disliked type hints. Working with CDK started to change that for the better and then I discovered [AWS Lambda Powertools](https://awslabs.github.io/aws-lambda-powertools-python/). It combines multiple things I am passionate about so I became passionate about using the library and improving it. Lambda Powertools utilizes static typing for several of its main features ([Parser](https://awslabs.github.io/aws-lambda-powertools-python/latest/utilities/parser/), [Event Sources](https://awslabs.github.io/aws-lambda-powertools-python/latest/utilities/data_classes/) and [Typing](https://awslabs.github.io/aws-lambda-powertools-python/latest/utilities/typing/)).

I was keen to contribute to this open-source project which meant I had to jump into static typing and contribute code that will blend in and be accepted. Using static types was so natural, it made so much sense, especially in a codebase that is unknown to me. Instead of being noise, those type hints were real **hints** that helped me understand a codebase I was unfamiliar with.

My stomach now stays perfectly calm while I'm reading or writing code like this:

```python
log_level: Optional[Union[int, str]] = None
```

Using CDK and contributing to Lambda Powertools made me turn a 180 degrees. I went from "I hate this..." to "type hints are great!". Using them made me understand why type hints and static typing are necessary and helpful. I even use them sporadically in my [pet projects](https://github.com/ivica-k) while trying to become a better developer.

---

Our ancestors cooked food which allowed for their digestive tracts to become smaller, leaving more energy for brain growth. Something similar happened to me; using type hints allowed me to understand unfamiliar codebases and improve my coding skills. My humble contributions are a way of paying back to the open-source community and thanking contributors of [Lambda Powertools for Python](https://awslabs.github.io/aws-lambda-powertools-python/)

A good way to end this article is with another Public Enemy reference: It took a [multi-billion dollar company with tens of thousands of developers](https://en.wikipedia.org/wiki/It_Takes_a_Nation_of_Millions_to_Hold_Us_Back) just a few months to convert this non-stubborn dynamic-typer into a typing hints aficionado.

No statically typed languages were hurt during the writing of this article.