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

some worlds: some typing in LocalRom #3090

Merged
merged 6 commits into from
May 17, 2024

Conversation

beauxq
Copy link
Collaborator

@beauxq beauxq commented Apr 4, 2024

What is this fixing or adding?

A few people have copied this code.
Maybe we can improve it before more people copy it.

I thought about putting it in Utils.py, but it has some SNES specific stuff, and other variations.
Also, I'm not sure how this landscape will change as people move towards not requiring roms for generation.

read_bytes

It's not safe to return bytearray when we think it's bytes

a = rom.read_bytes(8, 3)
hash(a)  # This won't crash, right?

I considered converting to bytes before return, but I think that would be a slow-down for no benefit.
It's already a slice, so there's no danger of mutating the source.
And some are likely to want bytearray, so they would make 2 conversions just to get back to where it started.

write_bytes

Some have found that they can pass a list[int], and it works.

Iterable[SupportsIndex] is what's required for bytearray.__setitem__(slice, values)
We need to add __len__ for the len(values) in this function.

The reason I didn't add the write_bytes typing to dkc3 and smw, is that they both didn't have any typing imports in this file, so I thought it would be a little more invasive to add them.

How was this tested?

unit tests and type checking

### `read_bytes`

It's not safe to return `bytearray` when we think it's `bytes`
```python
a = rom.read_bytes(8, 3)
hash(a)  # This won't crash, right?
```

### `write_bytes`

`Iterable[SupportsIndex]` is what's required for `bytearray.__setitem__(slice, values)`
We need to add `__len__` for the `len(values)` in this function.
@beauxq beauxq requested a review from Berserker66 as a code owner April 4, 2024 16:24
@github-actions github-actions bot added the waiting-on: peer-review Issue/PR has not been reviewed by enough people yet. label Apr 4, 2024
@beauxq
Copy link
Collaborator Author

beauxq commented Apr 4, 2024

world owners:
@Berserker66 @PoryGone @PinkSwitch

@PoryGone
Copy link
Collaborator

PoryGone commented Apr 4, 2024

Seems fine.

@Exempt-Medic Exempt-Medic added waiting-on: world-maintainer Issue/PR is waiting for feedback or approval by the maintainer of a world. is: enhancement Issues requesting new features or pull requests implementing new features. labels Apr 5, 2024
@PinkSwitch
Copy link
Contributor

I approve

Copy link
Contributor

@PinkSwitch PinkSwitch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I approve of this change

@ScipioWright ScipioWright added waiting-on: core-review Issue/PR has been peer-reviewed and is ready to be merged or needs input from a core maintainer. and removed waiting-on: world-maintainer Issue/PR is waiting for feedback or approval by the maintainer of a world. waiting-on: peer-review Issue/PR has not been reviewed by enough people yet. labels May 17, 2024
@black-sliver
Copy link
Member

black-sliver commented May 17, 2024

If you are worried about people copying this. Should we maybe remove the inheritance of object from them as well? This seems to be left over from python2 days.

Otherwise this looks good to me and should be safe since it only touches typing. I've never seen Collection[SupportsIndex], but I believe you've done your research there :D

@beauxq
Copy link
Collaborator Author

beauxq commented May 17, 2024

removed the object inheritance

In practice, we'll probably never see any difference between Collection[int] and Collection[SupportsIndex] and SupportsIndex comes from something that is poorly named, so I'm kind of on the fence between Collection[int] and Collection[SupportsIndex]

Copy link
Member

@black-sliver black-sliver left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While poorly named, if this is what our buffer calls it, we should use it and the world devs did not complain, so we good.

@black-sliver black-sliver merged commit 280b67f into ArchipelagoMW:main May 17, 2024
16 checks passed
jnschurig pushed a commit to Tranquilite0/Archipelago-SoulBlazer that referenced this pull request Jun 13, 2024
* some worlds: some typing in `LocalRom`

### `read_bytes`

It's not safe to return `bytearray` when we think it's `bytes`
```python
a = rom.read_bytes(8, 3)
hash(a)  # This won't crash, right?
```

### `write_bytes`

`Iterable[SupportsIndex]` is what's required for `bytearray.__setitem__(slice, values)`
We need to add `__len__` for the `len(values)` in this function.

* remove `object` inheritance
qwint pushed a commit to qwint/Archipelago that referenced this pull request Jun 24, 2024
* some worlds: some typing in `LocalRom`

### `read_bytes`

It's not safe to return `bytearray` when we think it's `bytes`
```python
a = rom.read_bytes(8, 3)
hash(a)  # This won't crash, right?
```

### `write_bytes`

`Iterable[SupportsIndex]` is what's required for `bytearray.__setitem__(slice, values)`
We need to add `__len__` for the `len(values)` in this function.

* remove `object` inheritance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
is: enhancement Issues requesting new features or pull requests implementing new features. waiting-on: core-review Issue/PR has been peer-reviewed and is ready to be merged or needs input from a core maintainer.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants