Skip to content

Structuring Generics #52

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

Closed
wakemaster39 opened this issue Nov 23, 2018 · 5 comments
Closed

Structuring Generics #52

wakemaster39 opened this issue Nov 23, 2018 · 5 comments

Comments

@wakemaster39
Copy link

I realized I forgot to file an issue before submitting a PR, but this is to capture #51 .

The problem I have experienced is complex object with generic can be tedious to make work. Take the below code as an example, to use cattrs I need to register a hook for type ~T which in this case would be assign it to int, or I could register a hook on M.

T = TypeVar("T")

@attrs(auto_attrib=True)
class M(Generic[T]):
    prop: List[T]

data = {
  "prop": [1,2,3]
}

result = structure(data, M)

If a TypeVar with the name T is used anywhere else in my code it must be of type int or structuring is going to have issues.; This gets even worse when you have nested generics that share the same type var name. I typically just use T more my generic variable name, so I run into this problem often.

To address this, what I would like to see cattrs support is passing a _GenericAlias into the structure function and automatically handle the structuring if possible. The code above would stay the same except the last line which would become

result = structure(data, M[str])

Given this support, I no longer need to manually register ~T or M and the existing structuring mechanisms could be utilized to handle the structuring. The mentioned PR is my first pass at implementing this feature, looking to see if this i something the library is willing support before I continue the work in polishing the code, documentation and tests.

@Tinche
Copy link
Member

Tinche commented Nov 25, 2018

Hi,

this is very interesting but I'm a little sick at the moment so it might take me a while to take a look. Thanks!

@wakemaster39
Copy link
Author

@Tinche have you had any time to take a look at this?

I have been playing around with it a bunch more and cleared out all of the issues that I have run into and can say it makes me love cattrs even more.

I have started to use it in some of my smaller systems to try and get some load against it and see if I have find any more problems. I can tell you being able to replace custom and nested deserializer with typing is about as good as it gets for me..

@wakemaster39
Copy link
Author

This had made it into our production environment for over 6 months now and things have been pretty stable so far. Going to be merging in the last changes to support python3.8 in the next couple days.

If this is something that is of interest, I will happily put in the work to merge #51. If not I can release my own forked library but would like to work towards getting this merged in here.

@Dr-ZeeD
Copy link

Dr-ZeeD commented Mar 8, 2021

I am currently running into the same issue and I am wondering how to tackle structuring and unstructuring of generics...

@wakemaster39
Copy link
Author

I should have closed this off once the PR was merged. structuring generics just works now.

https://github.com./Tinche/cattrs/blob/master/tests/test_generics.py

Here are some unit tests with it, I am going to close out my old ticket. I would open a new one if you are still having issues.

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

3 participants