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.extend() not type hint friendly?

See original GitHub issue

This may be an incorrect usage of App.extend() but when my CLI starts it creates an instance of a github client that gets used in a Github Cement controller:

app.extend("github", Github(access_token))

With this method of extending, I beleive you lose access for python’s type hints to be aware of what app.github is and therefore you lose things like autocompletion in VS Code.

Possibly there is a better way to handle this use case?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
tomekrcommented, Feb 4, 2021

Yea it looks like self.app.* gets lost as well. This is a screenshot from a newly generated todo tutorial app:

todo

Back in the top-level main.py it looks like app is an instance of MyApp but the type system can’t figure out the handlers.

toplevel

0reactions
coccoinomanecommented, Oct 31, 2022

Any feedback, ideas, solutions here would be greatly appreciated.

Hi @derks !

As a workaround, what about casting self.app.* to App? It works when done in the controller class:

Markup 2022-10-31 at 17 10 08

Code completion works up to the first level, meaning that app.self.* is discovered, but app.self.config.* isn’t. This could be solved:

  1. by also casting app’s attributes (eg. app.self.config and app.self.config.cache) to their type, but only after these attributes have been set; or
  2. by defining getter functions in the controller with an explict return type, for example:
    from cement.core.config import ConfigInterface
    from cement.core.cache import CacheInterface
    
    def get_config(self) -> ConfigInterface:
        return self.app.config
    
    def get_cache(self) -> CacheInterface:
        return self.app.cache
    

Cheers, Cocco

PS: Please note that, in the screenshot, I missed a super().__init__() in the first line of the __init__() method.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I extend class parameters with a method while ...
I get no type hints from typescript when I access them using indexing, and accessing them with dot syntax is impossible, because typescript ......
Read more >
Why optional type hinting in python is not that popular? - Reddit
I don't really get the arguments that it's so burdensome to add type hints. Typehints are entirely optional, and can be introduced as...
Read more >
[Proposal] Type-hint what can be type-hinted #1409 - GitHub
Of course I'm not proposing to force type-hinting stuff just for ... [5.8] Fix for container app()->call() method laravel/framework#26920.
Read more >
[Python-Dev] Type hints -- a mediocre programmer's reaction
I've heard this argument that, because the type hints are optional, they're not something that beginners or the average user needs to worry...
Read more >
Using Python's Type Annotations - DEV Community ‍ ‍
Type Annotating is a new feature added in PEP 484 that allows adding type hints to variables. They are used to inform someone...
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