Skip to content

memoryview not accepted in str constructor #3243

@anuraaga

Description

@anuraaga

Summary

It seems memoryview is rejected for str constructor with encoding param. AFAICT, this is allowed by Python.

buf = memoryview(b"hello world")
s = str(buf, "utf-8")
error[[invalid-argument-type](https://ty.dev/rules#invalid-argument-type)]: Argument to class `str` is incorrect
 --> str_memoryview.py:5:9
  |
3 | buf = memoryview(b"hello world")
4 |
5 | s = str(buf, "utf-8")
  |         ^^^ Expected `bytes | bytearray`, found `memoryview[int]`
6 | print(s)
  |
info: rule `invalid-argument-type` is enabled by default

https://docs.python.org/3/library/stdtypes.html#textseq

If at least one of encoding or errors is given, object should be a bytes-like object

https://docs.python.org/3/glossary.html#term-bytes-like-object

An object that supports the Buffer Protocol and can export a C-contiguous buffer. This includes all bytes, bytearray, and array.array objects, as well as many common memoryview objects.

I'm not sure what the typing distinction would be for a "common" vs "uncommon" memoryview as described here, but I guess memoryview[int] at the least is supposed to work.

Version

ty 0.0.29 (438a78d 2026-04-05)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions