
conveniently enough this came up last week in a little challenge I posted as part of my stream discord -- here's a proof of concept mypy plugin that I came up with (more on that in the video I posted: https://www.youtube.com/watch?v=KWjGyDflNKQ ) it could probably be generalized to some specific syntax or decorator or Annotated as well but this was just a small proof of concept ```python from __future__ import annotations from typing import Callable from mypy.plugin import Plugin from mypy.plugin import FunctionContext from mypy.types import Instance from mypy.types import LiteralType from mypy.types import Type class CustomPlugin(Plugin): def _require_only_literals(self, func: FunctionContext) -> Type: # invalid signature, but already handled if len(func.arg_types) != 1 or len(func.arg_types[0]) != 1: return func.default_return_type tp = func.arg_types[0][0] if ( not isinstance(tp, Instance) or not isinstance(tp.last_known_value, LiteralType) ): func.api.fail('expected string literal for argument 1', func.context) return func.default_return_type # already handled by signature # if f'{tp.type.module_name}.{tp.type.name}' != 'builtins.str': return func.default_return_type def get_function_hook( self, name: str, ) -> Callable[[FunctionContext], Type] | None: if name == 't.somefunc': return self._require_only_literals else: return None def plugin(version: str) -> type[Plugin]: return CustomPlugin ``` Anthony On Wed, Aug 4, 2021 at 7:15 PM Paul Bryan <pbryan@anode.ca> wrote:
I've learned something today. 🙂
On Wed, 2021-08-04 at 16:11 -0700, S Pradeep Kumar wrote:
I have implemented a similar check in our internal tools: you can only do a database query with a statically inferable string literal. I agree the feature would be useful more generally.
Jelle: Thanks, that's another good use case!
I'm open to other ways to denote arbitrary literal strings if you have ideas.
On Wed, Aug 4, 2021 at 3:29 PM Paul Bryan <pbryan@anode.ca> wrote:
On Wed, 2021-08-04 at 14:14 -0700, S Pradeep Kumar wrote:
Setting aside the question of whether it can easily be determined whether a given string is a literal or not (I don't know, but would be interested in knowing the answer)...
As per PEP 586:
Literal types indicate that a variable has a specific and concrete value. For example, if we define some variable foo to have type Literal[3], we are declaring that foo must be exactly equal to 3 and no other value.
Right, so we're on the same page, want to confirm that should be no rule dictating how 3 was arrived at. It could have been the result of addition. I expect Literal["aaa", "bbb"] would currently accept the string "aaa" regardless of whether it was "a"*3 , "a" + "aa", or "aaa".
Paul: No, only "aaa" is accepted. That's what I was trying to show with the Mypy snippet I linked :)
_______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: asottile@umich.edu