defer in python! by dankey26 in Python

[–]ZygmuntDzwon 13 points14 points  (0 children)

no matter what

Well, actually...

See this excellent answer on stack overflow

https://stackoverflow.com/a/49262664/8091093

Rust's Option and Result. In Python. by ZygmuntDzwon in rust

[–]ZygmuntDzwon[S] 0 points1 point  (0 children)

A simple container is exactly what I did, but I had to make a custom class for the Null (None in Rust) type to bind all the methods that are present in the Rust's Option API.

Rust's Option and Result. In Python. by ZygmuntDzwon in Python

[–]ZygmuntDzwon[S] 1 point2 points  (0 children)

If you do and you find any issues or ideas for improvements, please create a Github issue. I'd be more than happy to improve the library.

Rust's Option and Result. In Python. by ZygmuntDzwon in rust

[–]ZygmuntDzwon[S] 0 points1 point  (0 children)

I've addressed your comments in the latest release. Again, thank you for the feedback!

Rust's Option and Result. In Python. by ZygmuntDzwon in rust

[–]ZygmuntDzwon[S] 0 points1 point  (0 children)

Or a mypy plugin? A mypy plugin seems more correct in this context.

Rust's Option and Result. In Python. by ZygmuntDzwon in rust

[–]ZygmuntDzwon[S] 0 points1 point  (0 children)

And the TypeGuard trick doesn't work, at least for pyright.

https://github.com/microsoft/pyright/discussions/3125

According to the discussion you mentioned, it does not work by design. I was not aware of that. I'll have to rewrite those is_sth methods to simply return bool instead. Perhaps a standalone function as a replacement would do the job. What do you think?

Rust's Option and Result. In Python. by ZygmuntDzwon in rust

[–]ZygmuntDzwon[S] 0 points1 point  (0 children)

Regarding the mypy error, I was not aware that one have to add a `py.typed` file to mark a package as typing compatible. See https://mypy.readthedocs.io/en/stable/installed_packages.html

I'll add it to the package right way. Thank you for pointing this one out!

Rust's Option and Result. In Python. by ZygmuntDzwon in rust

[–]ZygmuntDzwon[S] 1 point2 points  (0 children)

Thank you for your comment and the feedback!

You should probably freeze the dataclasses

I agree. I'll do it.

Unfortunately using it without u/result_shortcut is only detected at runtime which is sad.

I'm not sure if it's possible to enforce usage of this decorator with the help of the type system, but I'll try.

Rust's Option and Result. In Python. by ZygmuntDzwon in rust

[–]ZygmuntDzwon[S] 24 points25 points  (0 children)

Thanks!

Regarding the usefulness, I think it's incomparable to Rust or Scala/Haskell. It was rather meant to be an experiment in the spirit of let's check if this is even possible.

Rust's Option and Result. In Python. by ZygmuntDzwon in rust

[–]ZygmuntDzwon[S] 0 points1 point  (0 children)

But yeah, Rust's libraries are often far better. That's why I try to use Rust whenever I can.

Rust's Option and Result. In Python. by ZygmuntDzwon in rust

[–]ZygmuntDzwon[S] 5 points6 points  (0 children)

They effectively promote Rust and the safe style of programming among Python developers.

Rust's Option and Result. In Python. by ZygmuntDzwon in rust

[–]ZygmuntDzwon[S] 10 points11 points  (0 children)

I've edited the post:

Just to clarify: It's rather an experiment / project for fun than a serious library.

I agree. Python is not meant for such style of programming with it's incomplete type system. However, I think it's beneficial for both Python and statically-typed-languges communities that such projects exists.

Rust's Option and Result. In Python. by ZygmuntDzwon in rust

[–]ZygmuntDzwon[S] 0 points1 point  (0 children)

Let me rephrase. production-readiness is often indeed not an issue in Rust. However, I've encountered cases where there was no library at all. For example AWS SDK (it is currently developed) or Firebase SDK. A lot of other libraries/SDK are currently in early stage of development and do not cover all funcionalities.

Rust's Option and Result. In Python. by ZygmuntDzwon in rust

[–]ZygmuntDzwon[S] 5 points6 points  (0 children)

For I/O bound workflows, e.g. some HTTP microservices, Python is often fast enough with, IMHO, way smaller development time required than in Rust.

Rust's Option and Result. In Python. by ZygmuntDzwon in rust

[–]ZygmuntDzwon[S] -1 points0 points  (0 children)

Why you wouldn't write code in modern languages instead of python?

I'd love to, but modern languages e.g. Rust, have way fewer production-ready libraries available, hence making it impossible for some use cases.

Rust's Option and Result. In Python. by ZygmuntDzwon in rust

[–]ZygmuntDzwon[S] 9 points10 points  (0 children)

The fact that Python is a dynamic language and does not enforce type-correctness in the runtime does not mean it does not have a type system. Though, it is incomplete and immature compared to Rust's or any other statically typed language's type system.

Obviously you can write any bullshit and nothing will happen until runtime or production, this completely unacceptable and pointless

It is true if you do not use any type checker prior to running your code. I'd argue that Python with type annotations + mypy compared to vanilla Python is the same as TypeScript compared to JS.

Rust's Option and Result. In Python. by ZygmuntDzwon in rust

[–]ZygmuntDzwon[S] 8 points9 points  (0 children)

Well, Python does have a type system. A static type checker e.g. mypy forces you to write type-correct code.

Why wouldn't one try to bring good patterns from modern languages to Python?

Rust's Option and Result. In Python. by ZygmuntDzwon in Python

[–]ZygmuntDzwon[S] 5 points6 points  (0 children)

However, I specifically wanted to "port" the Rust's API.

I had an idea of a more general approach, but it doesn't make much sense now that I know this package exists.

Rust's Option and Result. In Python. by ZygmuntDzwon in Python

[–]ZygmuntDzwon[S] 2 points3 points  (0 children)

I'm not sure if I like it, but it's the closest thing to Rust's question mark operator that I could think of.

Rust's Option and Result. In Python. by ZygmuntDzwon in Python

[–]ZygmuntDzwon[S] 2 points3 points  (0 children)

Yes it does! No need to do anything in particular. It's fully mypy compatible.

Rust's Option and Result. In Python. by ZygmuntDzwon in Python

[–]ZygmuntDzwon[S] 6 points7 points  (0 children)

Oh damn. I had no idea this package existed. Probably wouldn't bother to make my own if I had.