Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Aditya Gudimella
    @AdityaGudimella
    I don't think that will work with ParamSpec. It has also been deprecated. Thank you for the suggestion though!
    stdedos
    @stdedos:matrix.org
    [m]

    Hello there! Is there a reason that, when overriding a class method, e.g.class StopParsingEarlyAction(argparse.Action), I have to pedantically re-write

        def __call__(
            self,
            parser: argparse.ArgumentParser,
            namespace: argparse.Namespace,
            values: str | Sequence[Any] | None,
            option_string: str | None = None,
        ) -> NoReturn:

    even though "that's necessary by concept"? (I'm using --strict, error is no-untyped-def)

    Patrick Nicodemus
    @patrick-nicodemus
    I tried to run stubgen on pandas and got this in one of the files, timezones.pyi
    from typing import Any, ClassVar
    
    from typing import overload
    import datetime
    import dateutil.tz._common
    import dateutil.tz.tz
    import dateutil.tz.tz.__get_gettz.<locals>
    import pytz
    UTC: pytz.UTC
    dateutil_gettz: dateutil.tz.tz.__get_gettz.<locals>.GettzFunc
    dst_cache: dict
    import_optional_dependency: function
    mypy says this is an error. Is this a bug in stubgen or is <locals> a real thing
    I don't recognize angle bracket syntax in python
    Patrick Nicodemus
    @patrick-nicodemus
    Ok, I looked into the dateutil package and it has this
    
    def __get_gettz():
        tzlocal_classes = (tzlocal,)
        if tzwinlocal is not None:
            tzlocal_classes += (tzwinlocal,)
    
        class GettzFunc(object):
    So it's like a private class made private by burying its definition inside a function
    Patrick Nicodemus
    @patrick-nicodemus
    Is there a right way to do this in mypy? What would be the syntax to say an object is an inhabitant of a private class like this
    2 replies
    mwchase
    @mwchase
    My gut reaction is to look at how it's used and see if it's possible to write a decent Protocol for it to implement, but I don't know if that's feasible here.
    Looking at the documentation, I would expect Callable[[str | None], datetime.tzinfo] to work, but maybe I'm missing something.
    Or just defining it as a function, like it acts like for the docs.
    stdedos
    @stdedos:matrix.org
    [m]

    Hello there! I am wondering, what does that error mean?
    "Callable[..., None]" has no attribute "_original_name" [attr-defined] for the operation:
    func_new._original_name = func_new.__name__

    I thought that, according to PEP 232 (https://peps.python.org/pep-0232/), you can "freely" set whatever attributes you wanted to functions 😕

    stdedos
    @stdedos:matrix.org
    [m]
    On another topic, where can I see (online preferably) stdlib type-annotations?
    I'd think that sys.exit would be a -> NoReturn function, but IDE says it's -> None
    Alex Grönholm
    @agronholm
    @stdedos:matrix.org find the typeshed repository on github
    stdedos
    @stdedos:matrix.org
    [m]
    Alex Grönholm
    @agronholm
    yup
    stdedos
    @stdedos:matrix.org
    [m]
    I've tried the same with logging https://github.com/python/typeshed/blob/main/stdlib/logging/__init__.pyi, looking for _acquireLock and _releaseLock, which are missing 😕
    Alex Grönholm
    @agronholm
    private API is typically not included in typeshed; why do you need these?
    stdedos
    @stdedos:matrix.org
    [m]
    "I'm okay" if private API is private; however, I am trying to move into --strict.
    "Things like that" cause issues, and I'm not particularly happy in filling type: ignore all over the place 😕
    stdedos
    @stdedos:matrix.org
    [m]

    Also this output looks a little bit weird 😕

    def func1(list_of_files: list[os.PathLike[Any]]) -> list[os.PathLike[Any]]:
        scored = []
        for file in list_of_files:
            reveal_type(file)
            scored.append({"json": file, "score": random.randint(0, 9)})
    
        reveal_type(scored[0]["json"])
        return [scored_file["json"] for scored_file in scored]
    
    # test.py: note: In function "func1":
    # test.py:12: note: Revealed type is "os.PathLike[Any]"
    # test.py:15: note: Revealed type is "builtins.object"
    # test.py:16: error: List comprehension has incompatible type List[object]; expected List[PathLike[Any]]  [misc]

    Is it just me (i.e., can someone explain this to me), or is this a (known) issue of mypy? 😕

    Rickard Cronholm
    @cronholmrickard

    I'm trying to resolve arg-types using decorators, but not having any luck. Consider the following;

    from typing import Dict, Optional
    
    
    class Stuff:
        config: Optional[Dict] = None
    
        def __init__(self):
            pass
    
    
    class Runner:
        def __init__(self):
            pass
    
        def method(self, config: Dict):
            """docstring"""
            print(type(config))
            print(config)
    
    
    def decorator(func):
        def wrapper(*args, **kwargs):
            if not args[0].config:
                raise Exception("config may not be None")
            return func(*args, **kwargs)
    
        return wrapper
    
    
    @decorator
    def func(stuff: Stuff):
        runner = Runner()
        runner.method(stuff.config)
    
    
    def main():
        stuff = Stuff()
        stuff.config = None
        func(stuff)

    I get
    mypy_issue.py:33: error: Argument 1 to "method" of "Runner" has incompatible type "Optional[Dict[Any, Any]]"; expected "Dict[Any, Any]" [arg-type]

    It works as intended at runtime. How can I make mypy understand that the type checking is done in the decorator? If I move the exception into funcI don't get the mypy issue
    jean-paul.
    @jean-paul.:matrix.org
    [m]
    How do you annotate the return type of an async generator?
    Ah, look, there's typing.AsyncGenerator.
    Alex Grönholm
    @agronholm
    @jean-paul.:matrix.org collections.abc.AsyncGenerator is the non-deprecated version
    Kyle Altendorf
    @altendky
    https://mypy-play.net/?mypy=latest&python=3.11&gist=bbd2e5edf8b472aa6fe92ac734743e1e should there be a complaint about f(Bad()) since the parameter name on the method is wrong and won't work when f() calls p.m(x=3) that mypy isn't complaining about?
    2 replies
    stdedos
    @stdedos:matrix.org
    [m]
    2 replies
    stdedos
    @stdedos:matrix.org
    [m]
    Could mypy's message be more "hinting" to that fact?
    Or, in other words: As a mypy user, am I expected to know that I should do scored: list[DSTypedDict] = []?
    Alex Grönholm
    @agronholm
    yes
    mypy's error messages do leave a lot to be desired, yes
    stdedos
    @stdedos:matrix.org
    [m]
    I am confused now 😅
    • "Yes (I am expected to know)" sounds to me like --> Won't Fix / Works As Expected
    • "mypy's error messages do leave a lot to be desired" sounds to me like --> Maybe improvement request
    Alex Grönholm
    @agronholm
    mypy is correct in giving you an error but that error can be pretty useless or misleading at times
    Sam Wilson
    @sam.wilson:moobi.monster
    [m]
    Are mypy's ASTs traversed in the same order they are in the file (ie. line/column numbers always increasing)?
    (using, for example, ExtendedTraverserVisitor)
    Tristan Schulz
    @MrDiver
    Hello there, is anyone existing over here. I am from the manim dev team and im just going insane when using mypy or dmypy. I am running in to a problem where dmypy just completely ignores every configuration i throw at it and just scans the numpy site-package and errors out ... and i would really like to get it to work for the files i am actually working on https://github.com/ManimCommunity/manim/blob/main/.mypy.ini if you want to see the config it should be just fine but adding excludes and everything doesn't work it always throws the same error. And i mean the error is fine ... if it wouldn't be for the case that i am one ... running on python 3.10 two have passed --python-version 3.10 on multiple ways and also excluded site-packages over multiple ways ... how can i fix this ?
    "message": "Positional-only parameters are only supported in Python 3.8 and greater [syntax]",
    For further info i am running it inside a poetry environment
    Trying this since like 3 hours by now an i just have enough of it that it just doesn't work and ignores every single config option no matter what i do, either cli or the config file
    image.png
    image.png
    eo
    @eo:ong.ac
    [m]

    Hey :) Just a quick question: suppose I construct a set of classes like those below, such that I know MyClassInt is the only subclass of MyClass[Int].

    class MyClass(Generic[T], ABC):
        ...
    
    class MyClassInt(MyClass[Int]):
        ...

    Given this condition, MyClassInt and MyClass[Int] should be usable as type aliases for each other.

    I'm wondering if there's a way (possibly through a MyPy plugin) to declare such equivalences / rewrite rules? Essentially this would amount to allowing downcasts from MyClass[Int] to MyClassInt (which would always be safe as MyClassInt is the only non-abstract class under MyClass[Int]).

    1 reply
    Makarov Andrey
    @Winand
    image.png
    6 replies
    Hello. Is it somehow possible to use ctypes arrays in annotations without mypy errors? Python 3.8.15 + mypy.
    I think the answer is "no":-)
    jean-paul.
    @jean-paul.:matrix.org
    [m]
    Dependent types are an advanced type theory concept, I will be surprised if they show up in mypy in the next 5 years.
    Alexander Regueiro
    @alexreg
    Could anyone please tell me what the type signature for the implementation should be? Right now, I'm getting Overloaded function implementation does not accept all possible arguments of signature 1 [misc].
        T = TypeVar("T")
    
        @overload
        def param(self, name: str) -> Any:
            ...
    
        @overload
        def param(self, typ: Type[T], name: str) -> T:
            ...
    
        def param(self, typ: Type[T] | str, name: Optional[str]) -> T | Any:
            pass
    1 reply
    Shantanu
    @hauntsaninja
    @jean-paul.:matrix.org literal types are just simple dependent types ;-)
    ChungCheng Chuang
    @chuang16890_twitter
    L
    Patrick Nicodemus
    @patrick-nicodemus
    Martin-Lof published his paper on dependent type theory in 1972. Maybe an advanced concept but it has been around for a while now.