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

Support encoding string subclasses as keys in dicts #450

Closed
jcrist opened this issue Jun 23, 2023 · 2 comments · Fixed by #454
Closed

Support encoding string subclasses as keys in dicts #450

jcrist opened this issue Jun 23, 2023 · 2 comments · Fixed by #454

Comments

@jcrist
Copy link
Owner

jcrist commented Jun 23, 2023

Description

We should be able to support this case:

class mystr(str):
    pass

msgspec.json.encode({mystr("foo"): 1})
@BuzzCutNorman
Copy link

BuzzCutNorman commented Jun 23, 2023

This would be a great addition and would remove a pre-encoding processing step I need to do when SQLAlchemy results are present. Here is an example of the senario:

{'type': 'RECORD', 'stream': 'dbo-tags', 'record': {'Id': 1, 'TagName': '.net', 'Count': 329455, 'ExcerptPostId': 3624959, 'WikiPostId': 3607476}, 'time_extracted': DateTime(2023, 6, 21, 17, 11, 29, 11213, tzinfo=UTC)}

Here are all the key data types and values along with the value data types and values:

 KeyType: <class 'str'>, Key: type | Value Type: <class 'str'>, ValueType: RECORD
 KeyType: <class 'str'>, Key: stream | Value Type: <class 'str'>, ValueType: dbo-tags
 KeyType: <class 'str'>, Key: record | Value Type: <class 'dict'>, ValueType: {'Id': 1, 'TagName': '.net', 'Count': 329455, 'ExcerptPostId': 3624959, 'WikiPostId': 3607476}
 KeyType: <class 'sqlalchemy.sql.elements.quoted_name'>, Key: Id | Value Type: <class 'int'>, ValueType: 1
 KeyType: <class 'sqlalchemy.sql.elements.quoted_name'>, Key: TagName | Value Type: <class 'str'>, ValueType: .net
 KeyType: <class 'sqlalchemy.sql.elements.quoted_name'>, Key: Count | Value Type: <class 'int'>, ValueType: 329455
 KeyType: <class 'sqlalchemy.sql.elements.quoted_name'>, Key: ExcerptPostId | Value Type: <class 'int'>, ValueType: 3624959
 KeyType: <class 'sqlalchemy.sql.elements.quoted_name'>, Key: WikiPostId | Value Type: <class 'int'>, ValueType: 3607476
 KeyType: <class 'str'>, Key: time_extracted | Value Type: <class 'pendulum.datetime.DateTime'>, ValueType: 2023-06-21T17:11:29.011213+00:00

I run the row through the following pre-encoding processing:

        record = {}
        for k,v in row.items():
            record[str(k)] = v

Just in case it might be helpful here is a link to the quoted_name Class.
<class 'sqlalchemy.sql.elements.quoted_name'>

@jcrist
Copy link
Owner Author

jcrist commented Jun 24, 2023

This has been resolved in #454. If you want to try it out before the next release, you can install the main branch from GitHub.

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

Successfully merging a pull request may close this issue.

2 participants