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

thrift binary type converted into pyi string type #25

Closed
wdyin opened this issue Sep 9, 2021 · 10 comments · Fixed by #40
Closed

thrift binary type converted into pyi string type #25

wdyin opened this issue Sep 9, 2021 · 10 comments · Fixed by #40

Comments

@wdyin
Copy link

wdyin commented Sep 9, 2021

I really appreciate this awesome tool and it helped me a lot. But I just figure out that if a type is binary in thrift, it will be converted into str in pyi, instead of byte . I wonder if this behavior is expected.

@unmade
Copy link
Owner

unmade commented Sep 9, 2021

Hi, thanks for using it!

Can you please tell what thrift version you're using?

I do believe it is expected behaviour for thrifty (v1), but not for thriftpy2.
About a year ago I had this MR Thriftpy/thriftpy2#139

@wdyin
Copy link
Author

wdyin commented Sep 12, 2021

Yeah I'm using thriftpy2, so it's a little bit confusing when I see errors from pyright. Do you consider it reasonable to enable byte anno from binary? Actually thriftpy2 is used a lot.

@unmade
Copy link
Owner

unmade commented Sep 13, 2021

I see.
I will take a look at this in the next couple days

@wdyin
Copy link
Author

wdyin commented Jan 6, 2022

any follow-ups on this issue? thanks!

@unmade
Copy link
Owner

unmade commented Jan 7, 2022

I'm sorry it took me so long

Prior thriftpy2 v0.4.13 BINARY and STRING are the same types. Since v0.4.13 BINARY is indeed a separate type and will be cast to bytes by thrift-pyi.

So, my guess is that you should just update the thriftpy2 version to 0.4.13 or higher and you'll be fine

@sloane-shark
Copy link

sloane-shark commented Aug 15, 2022

We are using thriftpy v0.4.14 at work and still hitting this issue of binary types being inferred as str.

Example protocol:

struct Message {
  1: binary body,
  2: binary key,
  3: optional i32 partition = -1,
  4: optional i64 timestamp_micros = -1
}

Generated type:

@dataclass
class Message:
    body: Optional[str] = None
    key: Optional[str] = None
    partition: Optional[int] = -1
    timestamp_micros: Optional[int] = -1

interestingly, v0.4.14 has TType.BINARY and TType.STRING as aliases: https://github.com/Thriftpy/thriftpy2/blob/v0.4.14/thriftpy2/thrift.py#L98-L100

@unmade
Copy link
Owner

unmade commented Aug 17, 2022

@matthewess

As far as I can see they reverted separate BINARY type:

But right now it's back in the master branch, they just haven't made a release yet:

So for now we just have to wait for a new release, because there is no way to distinguish between STRING and BINARY

@JayH5
Copy link

JayH5 commented Nov 22, 2022

thriftpy2 now has a release with binary support (0.4.15/0.4.16). Do you plan to support this or are you open to pull requests?

@unmade
Copy link
Owner

unmade commented Nov 22, 2022

@JayH5
I'll add binary support, but it can take me a week or so to find some time. The project is open for pull requests, so feel free to create one

@unmade
Copy link
Owner

unmade commented Dec 12, 2022

I've added support for binary types
For thriftpy2 versions that define BINARY as STRING alias the result value will be str, for newer version - bytes

Feel free to re-open in case of any problem

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.

4 participants