question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

App: Raise a better error when `run` method not defined for the `LightningWork`

See original GitHub issue

🚀 Refactor

When a user forgets to define a run method in their LightningWork, a TypeError is raised:

Your Lightning App is starting. This won't take long.
ERROR: Found an exception when loading your application from bug.py. Please, resolve it to run your app.

Traceback (most recent call last):
  File "bug.py", line 19, in <module>
    app = L.LightningApp(Flow())
  File "bug.py", line 13, in __init__
    self.w = Work()
TypeError: Can't instantiate abstract class Work with abstract method run

Pitch

We should raise a better warning, pointing the users to the documentation on how to implement the run method.

Alternatives

Is there a reason why LightningWork is an abstract class? Can we make it a normal class like LightningFlow, and raise a NotImplementedError in the run method instead? (Note that LightningFlow is not an abstract class)

Additional context

As reported by @williamFalcon internally.


If you enjoy Lightning, check out our other projects! ⚡

  • Metrics: Machine learning metrics for distributed, scalable PyTorch applications.

  • Lite: enables pure PyTorch users to scale their existing code on any kind of device while retaining full control over their own loops and optimization logic.

  • Flash: The fastest way to get a Lightning baseline! A collection of tasks for fast prototyping, baselining, fine-tuning, and solving problems with deep learning.

  • Bolts: Pretrained SOTA Deep Learning models, callbacks, and more for research and production with PyTorch Lightning and PyTorch.

  • Lightning Transformers: Flexible interface for high-performance research using SOTA Transformers leveraging PyTorch Lightning, Transformers, and Hydra.

cc: @tchaton @hhsecond @Borda @ethanwharris @awaelchli

cc @justusschock @awaelchli @rohitgr7

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
awaelchlicommented, Sep 13, 2022

Is there a reason why LightningWork is an abstract class?

A LightningWork is not useful if run is not implemented, and we can’t have a default implementation. The class is marked abstract to signal to the user they need to implement run(). This marks the contract between user and framework: The user can subclass LightningWork and implements a valid run method. Then the framework can remotely execute the code in run().

Note that LightningFlow is not an abstract class)

There, the contract is different: The LightningApp class must call root_flow.run() at the root level, hence user needs to implement run for the LightningFlow at the root. But the children can have arbitrary methods, and the user is responsible for calling them in their tree of flows. One could make this stricter by marking run on the flow abstract, to signal a clear contract to the user.

We should raise a better warning, pointing the users to the documentation on how to implement the run method.

You can change the abstract method to one raising NotImplementedError, but IMO the static way is much better since it forces as stronger contract and lets IDE’s help you auto-complete the implementation directly, without you having to go to the docs learning “how to implement the run method”.

I’m ok with either way, as long as we force the contract.

1reaction
awaelchlicommented, Sep 16, 2022

Taking this one off your shoulders @krshrimali. See the two attached PRs 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Method not defined error - Salesforce Developer Community
Method not defined error. Hi, I'm trying to execute the below code in developer console anonymous window and I'm getting the error ....
Read more >
Path — lightning 1.8.3.post1 documentation
Return the relative path to another path identified by the passed arguments. If the operation is not possible (because this is not a...
Read more >
LWC Error: POSM_Item__c is not defined
Try setting POSM_Item__c to be an empty array by default? POSM_Item__c = []; . The error looks like it may be coming from...
Read more >
Python error: function not defined in class - Stack Overflow
That is to say, you would have defined the above methods as follows:: ... then on run:: def run(self): #do your stuff down...
Read more >
Click() method not working and Console returns an error ...
getElementsByClassName(…).click is not a function” error created while using click() method in JavaScript. The TypeError is an object that ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found