Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Ethan Smith
    @ethanhs
    In 3.9 and above you will be able to use collections.Counter as a generic.
    Neil Pilgrim
    @neiljp
    I'm looking forward to it, but still waiting to be relieved of 3.5 :)
    ...and an unhinted typing.Counter doesn't seem useful, unfortunately.
    Anyhow, good to know they plan to be drop-in compatible.
    Ethan Smith
    @ethanhs
    @neiljp I'm still confused, just use typing.Counter, you need not import collections.Counter
    Then you can annotate and instantiate Counter
    @neiljp If you need to annotate in 3.5, you can just use a type comment instead of a type annotation
    they are semantically identical almost completely
    Neil Pilgrim
    @neiljp

    Ah, sorry, I was confused by your use of the non-comment variable hinting syntax, which is not in 3.5 and so would not work for me (but which I'm aware of).

    from typing import Counter
    c = Counter[int]()

    This does work, though I think a type annotation is probably simpler.

    Anyhow, thanks for the discussion - it's good to know the intention to synchronize their behavior and make them more compatible :)
    Ethan Smith
    @ethanhs
    Sure thing!
    sarangsbabu367
    @sarangsbabu367
    I am getting module-stub not found for my project's modules, and it is really annoying. Any suggestions ?
    Ethan Smith
    @ethanhs
    @sarangsbabu367 what is the layout of your project?
    sarangsbabu367
    @sarangsbabu367
    @ethanhs I splitted the code into some modules. and these modules are used by importing to required locations.
    Ethan Smith
    @ethanhs
    where are they located?
    The same folder?
    @sarangsbabu367
    sarangsbabu367
    @sarangsbabu367
    @ethanhs I divided it accordingly. but i am getting this errors in some modules and not in some modules
    Ethan Smith
    @ethanhs
    @sarangsbabu367 without understanding how you are calling mypy, or the layout of the files, I can't help you, sorry
    Sigurd Ljødal
    @ljodal
    Hi, I’m playing around with an orm project where the goal is to statically type everything. I’d like to do something similar to Django’s F expression, but I can’t figure out a good way to type check this. An example is MyModel.select(alias=orm.ref('model_field')) where I’d like orm.ref('model_field') to be typed to return Expression[<type of field>]. In my mypy plugin I have the context I need in the get_method_hook for select(), but not in the get_function_hook for orm.ref(). Does anyone know if it’s possible to achieve this in mypy currently, or will I have to manually check everything in the method-callback?
    rpgoldman
    @rpgoldman
    I was looking at the TypedDict for a typed dictionary I have. One thing I don't understand from here is that it has a first argument that is a string. What is that for? Also, in the mypy docs, it is described as being intyping_extensions, but the StackExchange page that pointed me at TypedDict says to use mypy_extensions. Is mypy_extensions just an obsolete name for typing_extensions? Thanks!
    Sigurd Ljødal
    @ljodal
    @rpgoldman The first argument is for the name of the type that's created, similarly to how NamedTuple from collections works. I believe TypedDict started out in mypy_extensions, then moved to typing_extensions once standardized, and finally to the typing module starting from Python 3.8
    rpgoldman
    @rpgoldman
    @ljodal Thank you very much!
    Sigurd Ljødal
    @ljodal
    Regarding my question earlier I ended up maintaining a stack of TypeInfo objects where I push them in get_method_signature_hook and pop them in get_method_hook, as the get_function_hook calls where I need this context seems to be called between these two callbacks. If this is a Bad idea™️ and anyone has better suggestions I'd love some feedback
    Goldstein
    @GoldsteinE
    Hi. I want to subclass aiohttp.ClientSession and add one keyword argument to it's __init__ leaving others as-is. I want to avoid copying whole __init__ type signature from ClientSession. Can I somehow express that Subclass.__init__ is adding one argument to ClientSession.__init__ without changing others?
    Ethan Smith
    @ethanhs
    no, and I'm not sure that would be safe either
    Goldstein
    @GoldsteinE
    Ok.
    Also, can I express that class variable must be overriden in child class?
    Ben Scherrey
    @scherrey
    If class B inherits from class A and I've got a type spec of List["A"], why can't I pass an instance of "B" to it? How do I get typing to accept this? Running Python 3.7.
    Ethan Smith
    @ethanhs
    List is invariant
    Ben Scherrey
    @scherrey
    So is there a way to do this?
    Presumably Sequence?
    Sequence fixed the List[Type] issue but now I have a "Operation" has incompatible type "Callable[[Continuation], None]"; expected "Callable[[AF_Continuation], None]"
    Ben Scherrey
    @scherrey
    Continuation inherits from AF_Continuation in this case.
    mwchase
    @mwchase
    So, what does the callable in question do if you pass an AF_Continuation that is not a Continuation?
    Ben Scherrey
    @scherrey
    That works (in terms of type checking) except for the fact that my AF_Continuation doesn't have the members that Continuation has so the code isn't functional.
    So basically I need to go change all my parameters to AF_Continuation I presume... guess that makes sense.
    PoodleSkirt
    @PoodleSkirt2_twitter

    Hi friends. I've been looking into NewType recently, because I've been using the "builtins as a base class" approach to making subtypes to builtins, and I didn't know NewType existed: https://www.python.org/dev/peps/pep-0484/#newtype-helper-function

    From the docs, it says that the motivation for creating NewType is to minimize runtime overhead... should this actually be a concern of mine? Is there some reason that using subclasses of builtins (I know they don't need to be builtins but that's probably the most common use case) causes lots of runtime overhead? It just seems like it would be on par with the builtin itself, in which case it seems like there's not much reason to care about runtime overhead..

    David Tucker
    @dmtucker

    I'm having trouble figuring out how to use MYPYPATH...

    $ tree demo2/ stubs/
    demo2/
    └── foo.py
    stubs/
    └── foo.pyi
    
    0 directories, 2 files
    $ MYPYPATH="$PWD/stubs" venv/bin/mypy demo2
    demo2/foo.py:2: error: Incompatible return value type (got "int", expected "str")
    demo2/foo.py:5: error: Incompatible return value type (got "int", expected "str")
    Found 2 errors in 1 file (checked 1 source file)
    $ mv stubs/foo.pyi demo2/
    $ venv/bin/mypy demo2
    Success: no issues found in 1 source file

    What am I missing?

    PoodleSkirt
    @PoodleSkirt2_twitter
    Try putting a directory demo2 into your stubs dir and putting foo.pyi in that
    David Tucker
    @dmtucker

    Same deal

    $ tree demo2 stubs
    demo2
    └── foo.py
    stubs
    └── demo2
        └── foo.pyi
    
    1 directory, 2 files
    $ MYPYPATH="$PWD/stubs" venv/bin/mypy demo2
    demo2/foo.py:2: error: Incompatible return value type (got "int", expected "str")
    demo2/foo.py:5: error: Incompatible return value type (got "int", expected "str")
    Found 2 errors in 1 file (checked 1 source file)

    Does demo2 or foo.py need to be installed?

    PoodleSkirt
    @PoodleSkirt2_twitter
    Not sure what you mean by installed. Just curious, do you get the same output if you run MYPYPATH="$PWD/stubs" venv/bin/mypy demo2/foo.py, and MYPYPATH="./stubs" venv/bin/mypy demo2/foo.py?
    David Tucker
    @dmtucker
    Installed as in venv/bin/python -c 'import foo' doesn't fail, I guess. (Though, then I'd be curious about standalone scripts.)
    Those invocations seem to be the same (with and without a stubs/demo2 subdir).
    PoodleSkirt
    @PoodleSkirt2_twitter
    Hmm, my project does something extremely similar to yours except only using stubs for 3rd party libs. When I get to my work computer I'll try to make a stub for a file in the project itself and see if I have similar problems.
    Another thing I might try is putting __init__.py files in both the dir you're in and the demo2 dir and then try MYPYPATH="./stubs" venv/bin/mypy ., this is the way I usually run it
    PoodleSkirt
    @PoodleSkirt2_twitter
    Yeah after some experimentation I don't understand how to get mypy to see that a stubs directory has files that map to a local project (although as you note putting the .pyi files in the same dir as the .py files works)... also the fact that mypy will only check the stub files and not look at the normal .py files at all makes it seem not super useful to me anyway
    Owais Kazi
    @owaiskazi19
    Do I have to add python/mypy#8353 Reproducer code mentioned in the bug in the check-expressions.test file and run the code locally?
    Sigurd Ljødal
    @ljodal
    Should I be able to implement my own visitor and traverse part of the tree in mypy? Every time I try mypy just segfaults or when I try to subclass TraverserVisitor I get TypeError: interpreted classes cannot inherit from compiled
    jbwdevries
    @jbwdevries
    @dmtucker Do you have init files in the stubs?
    Jordan Ephron
    @JEphron

    I often come across a certain pattern in Python, where a subclass defines an inner class for the superclass to use.

    The following is an example of how I'd expect to use MyPy alongside that pattern:

    from typing import Generic, TypeVar, Type
    
    T = TypeVar("T")
    
    
    class Request(Generic[T]):
        Response: Type[T]
    
        def invoke(self) -> T:
            ...
    
     # since Response is defined in the body I'd expect MyPy to infer the type parameter (T)
    class MyRequest(Request):
        class Response:
            thing: int
    
    
    x = MyRequest().invoke().thing
    # x should have type `int`

    (This is a little like type members in languages like Scala)
    however, no inference is done. This isn't really a bug, but it makes this pattern somewhat cumbersome to annotate since now we're dealing with a forward reference:

    class MyRequest(Request["MyRequest.Response"]):
        ...

    There are a bunch of other cases where this sort of inference would be nice.
    How crazy would it be to infer T in the first case?