Skip to content

Conversation

@leonqadirie
Copy link

While returning a copy (not altering the original) makes more sense in the provided setting, this is:

  • not tested.
  • encourages just using the sorted() function instead of the copy() and sort() methods I suspect we want to see in this exercise - as it introduces the list method concept..

If we want to test for the returned value being a copy, keeping the original intact, this might be the corrected test:

@pytest.mark.task(taskno=7)
    def test_sorted_names(self):
        test_data = ["Steve", "Ultron", "Natasha", "Rocket"]
        test_data_copy = test_data.copy()
        expected = ["Natasha", "Rocket", "Steve", "Ultron"]
        actual_result = sorted_names(test_data)

        error_message = (
            f"Called sorted_names({test_data})."
            f"The function returned {actual_result}, but the tests "
            f"expected {expected}."
        )

        self.assertListEqual(actual_result, expected, msg=error_message)
        self.assertListEqual(
            test_data,
            test_data_copy,
            msg="Function should not modify the original list",
        )

While returning a copy makes more sense semantically, this is:
* not tested
* encourages just using the `sorted()` function instead of the `copy().sort()` method I suspect we want to see in this exercise.
@github-actions
Copy link
Contributor

github-actions bot commented Nov 7, 2024

Hello. Thanks for opening a PR on Exercism 🙂

We ask that all changes to Exercism are discussed on our Community Forum before being opened on GitHub. To enforce this, we automatically close all PRs that are submitted. That doesn't mean your PR is rejected but that we want the initial discussion about it to happen on our forum where a wide range of key contributors across the Exercism ecosystem can weigh in.

You can use this link to copy this into a new topic on the forum. If we decide the PR is appropriate, we'll reopen it and continue with it, so please don't delete your local branch.

If you're interested in learning more about this auto-responder, please read this blog post.


Note: If this PR has been pre-approved, please link back to this PR on the forum thread and a maintainer or staff member will reopen it.

@github-actions github-actions bot closed this Nov 7, 2024
@leonqadirie
Copy link
Author

leonqadirie commented Nov 7, 2024

Supersedes #3818.
Forum discussion here

@BethanyG
Copy link
Member

BethanyG commented Nov 7, 2024

Hi @leonqadirie 👋🏽

Thanks for submitting this. If students want to use sorted(), and understand what it does (returns a copy of said iterable), they are free to do so. It has the exact same effect as manually making a copy and then mutating that copy. Either is fine. So I think we leave the instructions as-is.

To your second point -- yes, we probably should be checking to see if the list was mutated. I will have a think on that one.

@leonqadirie
Copy link
Author

leonqadirie commented Nov 8, 2024

Hi @leonqadirie 👋🏽

Thanks for submitting this. If students want to use sorted(), and understand what it does (returns a copy of said iterable), they are free to do so. It has the exact same effect as manually making a copy and then mutating that copy. Either is fine. So I think we leave the instructions as-is.

To your second point -- yes, we probably should be checking to see if the list was mutated. I will have a think on that one.

Yes, I didn't want to suggest prohibiting sorted() or anything alike (my suggested test wouldn't check for implementation details like this), my suggestion was meant to focus on being explicit w.r.t. mutation (a topic somewhat dear to my heart). So either:

  • not explicitly asking for a copy, or
  • if explicitly asking for a copy, ensuring a copy is provided via tests

Thanks for consideration :)

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 this pull request may close these issues.

2 participants