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

Rewrite parts of the codebase to Rust #5

Open
lukipuki opened this issue Jan 14, 2024 · 0 comments
Open

Rewrite parts of the codebase to Rust #5

lukipuki opened this issue Jan 14, 2024 · 0 comments

Comments

@lukipuki
Copy link
Contributor

lukipuki commented Jan 14, 2024

As of writing this issue, GitHub shows that 10% of the codebase is written in Rust. This number should be higher, as Python has the following disadvantages compared to Rust.

  1. Weak type system: originally designed as a dynamic language, typing was added much later in Python 3.8. Type checking using mypy is still unable to figure out simple things. As this is a hobby project, weak typing support causes valuable time being lost on simple type errors that the compiler should be able to figure.
  2. Unsuitable for programming microcontrollers. There's MicroPython, but it's very basic with a lot of Python features missing. A big advantage of Python -- lots of libraries -- is not present in MicroPython, as libraries have to be rewritten just for MicroPython.

Type system weakness examples

Many libraries are not typed

pyudev, pyserial, gpiozero and other libraries are not typed yet.

Time handling

datetime.now() and datetime.now().astimezone() cannot be subtracted, as one is decorated with a timezone while the other isn't. Doing that results in a runtime error. In Rust, these are different types, so the error is caught already during compilation.

False negatives

Printing MAC address as a number in hex is not caught when the MAC address is already passed in as a string:

mac_addr = "abcdef012345"
f"{mac_addr:012x}"

False positives

On the other hand, there's also false negatives, like the following example.

def f(x: int) -> int | tuple[int, str]:
    if x == 0:
        return (0, "zero")
    return x


a, b = f(0)
c = f(1)

mypy complains that int is not iterable at the line a, b = f(0).

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

No branches or pull requests

1 participant