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

[stdlib] Implement List.__repr__() method #2634

Closed
wants to merge 6 commits into from

Conversation

msaelices
Copy link

Based on the List.__str__() one, to allow to call repr(some_list).

It's not currently passing because of the following error, but I think the code is correct and it's a limitation of the mojo compiler, which does not understand that, if self is made of RepresentableCollectionElement, the __repr__ method can call the __str__ one, as __type_of(self) will also be List[RepresentableCollectionElement]

image

@msaelices msaelices requested a review from a team as a code owner May 12, 2024 17:36
Signed-off-by: Manuel Saelices <msaelices@gmail.com>
@msaelices msaelices changed the title Implement List.__repr__() method [stdlib] Implement List.__repr__() method May 12, 2024
Copy link
Contributor

@gabrieldemarmiesse gabrieldemarmiesse left a comment

Choose a reason for hiding this comment

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

Thank you for the pull request! If you remove the trait, this PR should be good to merge :)

@@ -72,7 +72,9 @@ struct _ListIter[
return self.index


struct List[T: CollectionElement](CollectionElement, Sized, Boolable):
struct List[T: CollectionElement](
CollectionElement, Sized, Boolable, Representable
Copy link
Contributor

Choose a reason for hiding this comment

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

The list cannot be Representable yet because __repr__ is a staticmethod and not a method. We need conditional conformance to make List Representable conditionally. I think you can remove it.

Copy link
Author

Choose a reason for hiding this comment

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

@@ -31,6 +31,12 @@ fn test_variadic_list() raises:
check_list(5, 8, 6)


fn test_repr_list() raises:
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you also add a test case for an empty List? That would ensure the code doesn't break in the future.

Copy link
Author

Choose a reason for hiding this comment

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

Copy link
Contributor

@gabrieldemarmiesse gabrieldemarmiesse left a comment

Choose a reason for hiding this comment

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

Looks good! I think it's ready to be merged. Thank you for your contribution!

Copy link
Collaborator

@JoeLoser JoeLoser left a comment

Choose a reason for hiding this comment

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

Looks good, thank you! We have a trick proven out today for the List.index() method that I think we can apply here as well when the new nightly is available later tonight/tomorrow. I'll merge this internally in the meantime and we can refactor it as a static method to avoid the __type_of dance.

@JoeLoser JoeLoser self-assigned this May 16, 2024
@JoeLoser JoeLoser added the imported-internally Signals that a given pull request has been imported internally. label May 16, 2024
@modularbot
Copy link
Collaborator

✅🟣 This contribution has been merged 🟣✅

Your pull request has been merged to the internal upstream Mojo sources. It will be reflected here in the Mojo repository on the nightly branch during the next Mojo nightly release, typically within the next 24-48 hours.

We use Copybara to merge external contributions, click here to learn more.

@modularbot modularbot added the merged-internally Indicates that this pull request has been merged internally label May 16, 2024
modularbot pushed a commit that referenced this pull request May 16, 2024
[External] [stdlib] Implement List.__repr__() method

Based on the `List.__str__()` one, to allow to call `repr(some_list)`.

It's not currently passing because of the following error, but I think
the code is correct and it's a limitation of the mojo compiler, which
does not understand that, if `self` is made of
`RepresentableCollectionElement`, the `__repr__` method can call the
`__str__` one, as `__type_of(self)` will also be
`List[RepresentableCollectionElement]`

![image](https://github.com/modularml/mojo/assets/136875/b4fc0292-d2db-426e-8b61-33003e98de3d)

Co-authored-by: Manuel Saelices <msaelices@gmail.com>
Closes #2634
MODULAR_ORIG_COMMIT_REV_ID: b939d766211d3666fe65eea198f4bf24e734a180
@modularbot modularbot added the merged-externally Merged externally in public mojo repo label May 16, 2024
@modularbot
Copy link
Collaborator

Landed in ba59164! Thank you for your contribution 🎉

@modularbot modularbot closed this May 16, 2024
lsh pushed a commit to lsh/mojo that referenced this pull request May 17, 2024
[External] [stdlib] Implement List.__repr__() method

Based on the `List.__str__()` one, to allow to call `repr(some_list)`.

It's not currently passing because of the following error, but I think
the code is correct and it's a limitation of the mojo compiler, which
does not understand that, if `self` is made of
`RepresentableCollectionElement`, the `__repr__` method can call the
`__str__` one, as `__type_of(self)` will also be
`List[RepresentableCollectionElement]`

![image](https://github.com/modularml/mojo/assets/136875/b4fc0292-d2db-426e-8b61-33003e98de3d)

Co-authored-by: Manuel Saelices <msaelices@gmail.com>
Closes modularml#2634
MODULAR_ORIG_COMMIT_REV_ID: b939d766211d3666fe65eea198f4bf24e734a180

Signed-off-by: Lukas Hermann <lukashermann28@gmail.com>
msaelices added a commit to msaelices/mojo that referenced this pull request May 18, 2024
[External] [stdlib] Implement List.__repr__() method

Based on the `List.__str__()` one, to allow to call `repr(some_list)`.

It's not currently passing because of the following error, but I think
the code is correct and it's a limitation of the mojo compiler, which
does not understand that, if `self` is made of
`RepresentableCollectionElement`, the `__repr__` method can call the
`__str__` one, as `__type_of(self)` will also be
`List[RepresentableCollectionElement]`

![image](https://github.com/modularml/mojo/assets/136875/b4fc0292-d2db-426e-8b61-33003e98de3d)

Co-authored-by: Manuel Saelices <msaelices@gmail.com>
Closes modularml#2634
MODULAR_ORIG_COMMIT_REV_ID: b939d766211d3666fe65eea198f4bf24e734a180
martinvuyk pushed a commit to martinvuyk/mojo that referenced this pull request May 24, 2024
[External] [stdlib] Implement List.__repr__() method

Based on the `List.__str__()` one, to allow to call `repr(some_list)`.

It's not currently passing because of the following error, but I think
the code is correct and it's a limitation of the mojo compiler, which
does not understand that, if `self` is made of
`RepresentableCollectionElement`, the `__repr__` method can call the
`__str__` one, as `__type_of(self)` will also be
`List[RepresentableCollectionElement]`

![image](https://github.com/modularml/mojo/assets/136875/b4fc0292-d2db-426e-8b61-33003e98de3d)

Co-authored-by: Manuel Saelices <msaelices@gmail.com>
Closes modularml#2634
MODULAR_ORIG_COMMIT_REV_ID: b939d766211d3666fe65eea198f4bf24e734a180
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
imported-internally Signals that a given pull request has been imported internally. merged-externally Merged externally in public mojo repo merged-internally Indicates that this pull request has been merged internally
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants