Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to annotation type?
#2
Example with first function,the Tuple[Optional is not needed if use Python 3.10 or newer.
There a two return,so need to address both in Return type.
# pre.py
from typing import AnyStr

def func(a: AnyStr, *b: AnyStr) -> tuple[AnyStr, AnyStr] | tuple[None, AnyStr]:
    for _b in b:
        if a.startswith(_b):
            return _b, a.removeprefix(_b)
    return None, a

a = 'test123'
b = 'test1'
print(func(a, b))
Test using mypy.
G:\div_code\hex
λ mypy pre.py
Success: no issues found in 1 source file

G:\div_code\hex
λ python pre.py
('test1', '23')
So now it work for string and bytes.

Don't know about your converting in last part,would add that in new function that if needed.
Will get less readable if clutter to much in and the addition difficulty to get Type hint to work together.
Reply


Messages In This Thread
How to annotation type? - by gamecss - May-25-2023, 03:28 AM
RE: How to annotation type? - by snippsat - May-25-2023, 10:47 AM
RE: How to annotation type? - by gamecss - May-25-2023, 12:26 PM
RE: How to annotation type? - by snippsat - May-25-2023, 03:02 PM
RE: How to annotation type? - by gamecss - Jul-27-2023, 03:03 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Decorators @ annotation drcl 3 1,574 Feb-24-2024, 06:12 AM
Last Post: Gribouillis
  How to extract and label audio using timestamp annotation Mergorine 0 4,556 Nov-30-2020, 04:44 PM
Last Post: Mergorine
  Type hinting - return type based on parameter micseydel 2 3,180 Jan-14-2020, 01:20 AM
Last Post: micseydel
  Function Annotation got NameError: name 'xxx' is not defined Lance 6 7,728 Oct-23-2019, 03:13 AM
Last Post: Lance

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020