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

replace clone to clone_shallow and copy only as much as you need attributes. #618

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/poetry/core/packages/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def deactivate(self) -> None:
self._activated = False

def with_constraint(self: T, constraint: str | VersionConstraint) -> T:
dependency = self.clone()
dependency = self.clone_shallow()
dependency.constraint = constraint # type: ignore[assignment]
return dependency

Expand Down
8 changes: 5 additions & 3 deletions src/poetry/core/packages/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ def without_dependency_groups(self: T, groups: Collection[str]) -> T:
"""
Returns a clone of the package with the given dependency groups excluded.
"""
package = self.clone()
package = self.clone_shallow()

for group_name in groups:
if group_name in package._dependency_groups:
Expand All @@ -481,7 +481,8 @@ def without_optional_dependency_groups(self: T) -> T:
"""
Returns a clone of the package without optional dependency groups.
"""
package = self.clone()
package = self.clone_shallow()
package._dependency_groups = package._dependency_groups.copy()
Copy link
Contributor

Choose a reason for hiding this comment

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

a little comment explaining why we are copying the package._depndency_groups wouldbe great


for group_name, group in self._dependency_groups.items():
if group.is_optional():
Expand All @@ -500,7 +501,8 @@ def with_dependency_groups(

If `only` is set to True, then only the given groups will be selected.
"""
package = self.clone()
package = self.clone_shallow()
package._dependency_groups = package._dependency_groups.copy()

for group_name, group in self._dependency_groups.items():
if (only or group.is_optional()) and group_name not in groups:
Expand Down
5 changes: 4 additions & 1 deletion src/poetry/core/packages/specification.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,11 @@ def is_same_package_as(self, other: PackageSpecification) -> bool:
def clone(self: T) -> T:
return copy.deepcopy(self)

def clone_shallow(self: T) -> T:
return copy.copy(self)

def with_features(self: T, features: Iterable[str]) -> T:
package = self.clone()
package = self.clone_shallow()

package._features = frozenset(
canonicalize_name(feature) for feature in features
Expand Down