Re: mypy question

2024-01-13 Thread Karsten Hilbert via Python-list
Am Sat, Jan 13, 2024 at 09:20:00PM +0100 schrieb Karsten Hilbert via Python-list: > > I was wondering if > > your type hint for queries shouldn't be the following. > > > > queries:list[dict[str,str]|dict[str,list]|dict[str,dict[str, dict[str, > > Ant]]] Wait, not really. Let me give an

Re: mypy question

2024-01-13 Thread Karsten Hilbert via Python-list
Am Fri, Jan 12, 2024 at 02:23:43PM +0100 schrieb Antoon Pardon via Python-list: > > queries:list[dict[str, str | list | dict[str, Any]]]=None, > > > >into > > > > "List[Dict[str, Union[str, List[Any], Dict[str, Any" > > > >seems accurate. I just don't understand why list[dict[str, >

Re: mypy question

2024-01-12 Thread Antoon Pardon via Python-list
Op 29/12/2023 om 16:02 schreef Karsten Hilbert via Python-list: Am Fri, Dec 29, 2023 at 07:49:17AM -0700 schrieb Mats Wichmann via Python-list: I am not sure why mypy thinks this gmPG2.py:554: error: Argument "queries" to "run_rw_queries" has incompatible type "List[Dict[str, str]]";

Re: mypy question

2023-12-31 Thread Karsten Hilbert via Python-list
Thanks to all. I ended up using Sequence for the list part and Mapping for the dict part, which does require "import typing" which I would rather have avoided. Karsten -- GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B -- https://mail.python.org/mailman/listinfo/python-list

Re: mypy question

2023-12-30 Thread Greg Ewing via Python-list
On 31/12/23 10:06 am, Thomas Passin wrote: my suggestion above does work, *except* that you cannot mix-and-match different DictTypex types Have you tried declaring the argument as a Mapping instead of a dict? Seeing as Thomas Passin's Sequence experiment worked, it seems like this should work

Re: Aw: Re: mypy question

2023-12-30 Thread Greg Ewing via Python-list
On 31/12/23 8:05 am, Chris Angelico wrote: Ah, I think you've hit on the problem there. Consider this: def add_item(stuff: dict[str: str | int]): stuff["spam"] = "ham" stuff["vooom"] = 1_000_000 Yep, that's it exactly. It's not the union itself that's the problem, but the fact that

Re: mypy question

2023-12-30 Thread Thomas Passin via Python-list
On 12/30/2023 9:14 AM, Thomas Passin via Python-list wrote: On 12/29/2023 10:02 AM, Karsten Hilbert via Python-list wrote: I agree that mypy's grasp of my intent from queries:list[dict[str, str | list | dict[str, Any]]]=None, into "List[Dict[str, Union[str, List[Any], Dict[str,

Re: Aw: Re: mypy question

2023-12-30 Thread Chris Angelico via Python-list
On Sun, 31 Dec 2023 at 03:38, Thomas Passin via Python-list wrote: > I am not very expert in Python type hints. In working up the example > program I just posted, I got an error message from mypy that remarked > that "list" is invariant, and to try Sequence which is "covariant". I > don't know

Re: mypy question

2023-12-30 Thread Barry via Python-list
> On 30 Dec 2023, at 15:11, Karsten Hilbert via Python-list > wrote: > > queries = [{'SQL': 'SELECT %(value)s', 'args': {'value': 1}}] > > and > > run_queries(conn, queries:list[str|dict[str, Any]]): In cases like this I often use a wrapper class in place of a simple str. If you have a

Aw: Re: Re: mypy question

2023-12-30 Thread Karsten Hilbert via Python-list
> I'm fairly sure your database queries don't actually give you strings or > dicts, right? You probably get lists (or iterators) of tuples and > somewhere you convert them to the arguments you are feeding to > run_queries(). Ah, no, those queries are enshrined within the middleware as Python

Re: mypy question

2023-12-30 Thread Thomas Passin via Python-list
On 12/29/2023 10:02 AM, Karsten Hilbert via Python-list wrote: Am Fri, Dec 29, 2023 at 07:49:17AM -0700 schrieb Mats Wichmann via Python-list: I am not sure why mypy thinks this gmPG2.py:554: error: Argument "queries" to "run_rw_queries" has incompatible type "List[Dict[str, str]]"; expected

Aw: Re: Re: mypy question

2023-12-30 Thread Karsten Hilbert via Python-list
> It occurs to me that you could simplify things if you converted those > plain query strings to dicts: > > 'SELECT 1' --> {'SQL': 'SELECT 1'} Ha, indeed. There's likely not that many "simple string SQL queries" in that codebase so I shall take it as an opportunity to refactor them. So, at least

Re: Aw: Re: mypy question

2023-12-30 Thread Thomas Passin via Python-list
On 12/30/2023 10:08 AM, Karsten Hilbert via Python-list wrote: Dear Thomas, thanks for taking the time to look into my issue. Maybe it helps if I explain what I want (sorry that my web mailer does not respect indentation, I will insert dots). I want a function to run SQL queries:

Re: Aw: Re: mypy question

2023-12-30 Thread Thomas Passin via Python-list
On 12/30/2023 10:08 AM, Karsten Hilbert via Python-list wrote: Dear Thomas, thanks for taking the time to look into my issue. Maybe it helps if I explain what I want (sorry that my web mailer does not respect indentation, I will insert dots). I want a function to run SQL queries:

Aw: Re: mypy question

2023-12-30 Thread Karsten Hilbert via Python-list
Dear Thomas, thanks for taking the time to look into my issue. Maybe it helps if I explain what I want (sorry that my web mailer does not respect indentation, I will insert dots). I want a function to run SQL queries: run_queries(conn, queries): ...for q in queries: ..conn.execute(q) I

Re: mypy question

2023-12-30 Thread Thomas Passin via Python-list
On 12/29/2023 10:02 AM, Karsten Hilbert via Python-list wrote: I agree that mypy's grasp of my intent from queries:list[dict[str, str | list | dict[str, Any]]]=None, into "List[Dict[str, Union[str, List[Any], Dict[str, Any" seems accurate. I just don't understand why

Aw: Re: mypy question

2023-12-30 Thread Karsten Hilbert via Python-list
Hi Greg, > dict[str, str] is not a subtype of dict[str, str | something_else] > because you can assign a value of type something_else to the latter > but not the former. I understand what you are saying but I do not yet understand why this applies to my situation. I don't have Python at hand

Re: mypy question

2023-12-29 Thread Greg Ewing via Python-list
On 30/12/23 4:02 am, Karsten Hilbert wrote: def run_rw_queries ( link_obj:_TLnkObj=None, queries:list[dict[str, str | list | dict[str, Any]]]=None, Given that I would have thought that passing in list[dict[str, str]] for "queries" ought to be type

Re: mypy question

2023-12-29 Thread Karsten Hilbert via Python-list
Am Fri, Dec 29, 2023 at 11:04:59AM -0700 schrieb Mats Wichmann via Python-list: > >For what it's worth here's the signature of that function: > > > > def run_rw_queries ( > > link_obj:_TLnkObj=None, > > queries:list[dict[str, str | list | dict[str, Any]]]=None, > >

Re: mypy question

2023-12-29 Thread Mats Wichmann via Python-list
On 12/29/23 08:02, Karsten Hilbert via Python-list wrote: Dict[str, str] means the key type and value type should both be strings, Indeed, I know that much, list[dict[str, str]] is what is getting passed in in this particular invocation of run_rw_queries(). For what it's worth here's the

Re: mypy question

2023-12-29 Thread Karsten Hilbert via Python-list
Am Fri, Dec 29, 2023 at 07:49:17AM -0700 schrieb Mats Wichmann via Python-list: > >I am not sure why mypy thinks this > > > >gmPG2.py:554: error: Argument "queries" to "run_rw_queries" has incompatible > >type "List[Dict[str, str]]"; expected > >"List[Dict[str, Union[str, List[Any], Dict[str,

Re: mypy question

2023-12-29 Thread Mats Wichmann via Python-list
On 12/29/23 05:15, Karsten Hilbert via Python-list wrote: Hi all, I am not sure why mypy thinks this gmPG2.py:554: error: Argument "queries" to "run_rw_queries" has incompatible type "List[Dict[str, str]]"; expected "List[Dict[str, Union[str, List[Any], Dict[str, Any" [arg-type]

Re: mypy question

2023-12-29 Thread Karsten Hilbert via Python-list
Am Fri, Dec 29, 2023 at 01:15:29PM +0100 schrieb Karsten Hilbert via Python-list: > I am not sure why mypy thinks this > > gmPG2.py:554: error: Argument "queries" to "run_rw_queries" has incompatible > type "List[Dict[str, str]]"; expected > "List[Dict[str, Union[str, List[Any], Dict[str,