Skip to content

Fix(graph): Allow any Line subclass as edge_type in Graph/DiGraph #4251

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

Merged
merged 2 commits into from
May 27, 2025

Conversation

Akshat-Mishra-py
Copy link
Contributor

@Akshat-Mishra-py Akshat-Mishra-py commented May 18, 2025

Overview: What does this pull request change?

Fix #4250

More broadly, this fix allows any subclass of Line to be used as an edge_type in the Graph and DiGraph, making the implementation more flexible and extensible.

Motivation and Explanation: Why and how do your changes improve the library?

Previously, the Graph and DiGraph classes assumed that the edge_type will always have its first two arguments as start and end point.
But, such is not true for subclasses of Line. This change lets user use any subclass of Line in Graph and DiGraph.

Changes

  • Refactored Graph and DiGraph:
    • Fixed edge handling logic to support all subclasses of Line, not just LabeledLine.
    • Modified _add_edge accordingly.
  • Testing:
    • Added/updated tests for LabeledLine as edge types in test_graph.py.

Reviewer Checklist

  • The PR title is descriptive enough for the changelog, and the PR is labeled correctly
  • If applicable: newly added non-private functions and classes have a docstring including a short summary and a PARAMETERS section
  • If applicable: newly added functions and classes are tested

@github-project-automation github-project-automation bot moved this to 🆕 New in Dev Board May 18, 2025
@github-project-automation github-project-automation bot moved this from 🆕 New to ❌ Rejected in Dev Board May 18, 2025
@github-project-automation github-project-automation bot moved this from ❌ Rejected to 📋 Backlog in Dev Board May 18, 2025
@@ -198,7 +198,7 @@ def open_file(file_path: Path, in_browser: bool = False) -> None:
if current_os == "Windows":
# The method os.startfile is only available in Windows,
# ignoring type error caused by this.
os.startfile(file_path if not in_browser else file_path.parent) # type: ignore[attr-defined]
Copy link

Choose a reason for hiding this comment

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

Was it really unused? Maybe some static analysis tool was using it?

Copy link

Choose a reason for hiding this comment

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

Anyway I think it should not belong to this PR. Lets keep things clean.
Just do git rebase and remove the commit, instead of doing revert commit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

mypy was failing due to this.

Copy link

Choose a reason for hiding this comment

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

Oh, I need to teach myself what is mypy :P

Copy link
Contributor Author

Choose a reason for hiding this comment

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

mypy is failing on my local system because I am using windows which can be ignored now that I think I would need to reintroduce the comment to pass the checks

@mdrost
Copy link

mdrost commented May 18, 2025

I think this PR should not be solely about supporting LabeledLine but be about generic support of classes based on Line as author intended in the Graph API.
As such it would be nice to test more classes inheriting from Line e.g. DashedLine and maybe something more for completeness. Here we could have more generic test that loops around map of classes with its arguments. Or maybe unit test framework support some more sophisticated parameterized tests, I don't know.
Maybe do git rebase and squish commits with fixes and tests, to avoid explosion of commits in the history.

Nice Work.

Fix(graph): LabeledLine can now be used in _add_edge
…and DiGraph

Fix(test_graph): Fixed test_graph_accepts_labeledline_as_edge_type() always failing.
@Akshat-Mishra-py Akshat-Mishra-py force-pushed the fix/labeledline-graph branch from 4b264ae to fddf53a Compare May 18, 2025 11:50
@Akshat-Mishra-py Akshat-Mishra-py requested a review from mdrost May 18, 2025 12:10
@Akshat-Mishra-py
Copy link
Contributor Author

I think this PR should not be solely about supporting LabeledLine but be about generic support of classes based on Line as author intended in the Graph API. As such it would be nice to test more classes inheriting from Line e.g. DashedLine and maybe something more for completeness. Here we could have more generic test that loops around map of classes with its arguments. Or maybe unit test framework support some more sophisticated parameterized tests, I don't know. Maybe do git rebase and squish commits with fixes and tests, to avoid explosion of commits in the history.

Nice Work.

I do understand that this PR fixes all the Line subclass to be used in Graph and DiGraph but for checking every subclass of Line should i create a new test file from scratch or include it in the same test_graph.py or create a new test file I can use Line.__subclasses__() to automate the tests but we will need to import all the subclasses manually to create such a test as Line.__subclasses__() this will not List subclasses if they are not manually imported first.

@mdrost
Copy link

mdrost commented May 19, 2025

I can use Line.__subclasses__() to automate the tests but we will need to import all the subclasses manually to create such a test as Line.__subclasses__() this will not List subclasses if they are not manually imported first.

Testing every class from Line.__subclasses__() would be impossible as each class requires own set of edge_config arguments.

should i create a new test file from scratch or include it in the same test_graph.py

I think it would be enough to just test few more classes inside test_graph.py def test_graph_accepts_labeledline_as_edge_type (and rename the test accordingly) such as DashedLine and DoubleArrow that seems to be good candidates to be regularly used by users.

btw. Accepting PR is not up to me and as such you might need to wait some time for main developers.

Copy link
Contributor

@chopan050 chopan050 left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for allowing the use of v0.19.0's new LabeledLine in graphs.

Technically, the rest of Line's subclasses, such as DashedLine, Arrow and DoubleArrow, are fine, because their first parameters were also start and end. Regardless of that, it's always better to be more explicit.

If possible, could you please also add tests for some other Line subclasses as mdrost suggested, just in case?

@github-project-automation github-project-automation bot moved this from 📋 Backlog to 👍 To be merged in Dev Board May 21, 2025
@Akshat-Mishra-py
Copy link
Contributor Author

LGTM! Thanks for allowing the use of v0.19.0's new LabeledLine in graphs.

Technically, the rest of Line's subclasses, such as DashedLine, Arrow and DoubleArrow, are fine, because their first parameters were also start and end. Regardless of that, it's always better to be more explicit.

If possible, could you please also add tests for some other Line subclasses as mdrost suggested, just in case?

Ok I will do that and push the changes, thank you for reviewing.

Copy link
Member

@behackl behackl 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 very much!

@behackl behackl merged commit bcab73a into ManimCommunity:main May 27, 2025
24 checks passed
@github-project-automation github-project-automation bot moved this from 👍 To be merged to ✅ Done in Dev Board May 27, 2025
@behackl behackl added the enhancement Additions and improvements in general label May 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Additions and improvements in general
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Graph assumes that first two arguments of edge_type.__init__ are start and end parameters
4 participants