Skip to content

Conversation

@Bibo-Joshi
Copy link
Member

Effectively just uses a bit of Any to get a best effort result.

What we'd like to do is to infer the type of CC.job_queue from the Application. However, this is problematic, as the app has a type for the used (sub)class of CallbackContext itself. in particular, when speciyinf a subclass with ContextTypes, we'd like to somehow say CallbackContext[ExtBot, dict, dict, dict, TypeVar('JQ')] + App[…, JobQueue] = CC[…, JobQueue]. Similar things happen on CC.__init__/from_*.
I don't see how this can be achieved with the current status of the typing module, as neither

CCT = TypeVar('CCT', bound=CallbackContext)
def foo(arg: CCT[BT, UD, CD, BD, JQ]):
    pass

nor something like

def foo(arg: CCT) -> CCT.JQ:
   pass

are currently supported.

Hence, CallbackContext.job_queue stays annotated as Optional[JobQueue].

@harshil21 if you can test if pyright works with this as well, that would be nice. We can then maybe merge into #3017 and then merge both together into master.

Example snippet of what I've tested with mypy:

from telegram.ext import ApplicationBuilder, ContextTypes, CallbackContext, ExtBot


class CustomContext(CallbackContext[ExtBot, int, dict, dict]):
    pass


context_types = ContextTypes(user_data=int, context=CustomContext)
app = (
    ApplicationBuilder()
    .token("123456048:AAGpuiaeuiaeuiaeuiaev76jqr45aA")
    .context_types(context_types)
    .job_queue(None)
    .build()
)
reveal_type(app)
reveal_type(app.context_types)
context = CallbackContext(application=app)
reveal_type(context)
reveal_type(context.job_queue)


reveal_type(CallbackContext.from_update(1, app))
reveal_type(CustomContext.from_update(1, app))

@Bibo-Joshi Bibo-Joshi added the ⚙️ type-hinting affected functionality: type-hinting label May 11, 2022
@Bibo-Joshi Bibo-Joshi added this to the v20.0a1 milestone May 11, 2022
@Bibo-Joshi Bibo-Joshi merged commit 7dcaf54 into fix-type-hint May 12, 2022
@Bibo-Joshi Bibo-Joshi deleted the cct-init branch May 12, 2022 16:20
Bibo-Joshi added a commit that referenced this pull request May 12, 2022
…Alias to `ContextTypes.DEFAULT_TYPE` (#3017, #3023)

Co-authored-by: Bibo-Joshi <22366557+Bibo-Joshi@users.noreply.github.com>
@github-actions github-actions bot locked and limited conversation to collaborators May 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

⚙️ type-hinting affected functionality: type-hinting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants