GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add type annotations to functions? · Issue #179 · python-graphblas/python-graphblas

Add type annotations to functions? #179

New issue
Copy link
New issue
Copy link
Open
Open
Add type annotations to functions?#179
Copy link
Labels
discussionDiscussing a topic with no specific actions yetDiscussing a topic with no specific actions yetgood first issueClearly described and easy to accomplish. Good for beginners to the project.Clearly described and easy to accomplish. Good for beginners to the project.hygieneImprove code quality and reduce maintenance overheadImprove code quality and reduce maintenance overheadlowpriority

Description

@eriknw
eriknw
opened on Mar 17, 2022
Issue body actions

This would probably be straightforward to do. For functions that accept matrix expressions and can auto-compute, I would probably want a MatrixType that is a union of e.g. Matrix, TransposedMatrix, MatrixInfixExpr, MatrixExpression, and MatrixIndexExpr.

If we add type annotations, I would want to run mypy in CI.

What do y'all think? I wouldn't mind seeing this added, but I'm unlikely to add it any time soon.

Activity

  1. eriknw
    added
    good first issueClearly described and easy to accomplish. Good for beginners to the project.Clearly described and easy to accomplish. Good for beginners to the project.
    discussionDiscussing a topic with no specific actions yetDiscussing a topic with no specific actions yet
    hygieneImprove code quality and reduce maintenance overheadImprove code quality and reduce maintenance overhead
    on Mar 17, 2022
  2. eriknw commented on Mar 18, 2022

    @eriknw
    eriknw
    on Mar 18, 2022
    MemberAuthor
    More actions

    Actually, I think this would be really great to have. I'd even be in favor of adding type annotations to class attributes where it makes sense. Having type annotations would probably be helpful for the long tail of maintenance and to help new contributors (heh, or returning contributors) figure out what's going on. I wouldn't be surprised if it caught a bug or two too.

    So, big +1 from me. Any volunteers?

    Reacted by Sultan Orazbayev
  3. eriknw
    added
    lowpriority
    on Oct 27, 2022
  4. SultanOrazbayev commented on Nov 1, 2022

    @SultanOrazbayev
    SultanOrazbayev
    on Nov 1, 2022
    Member
    More actions

    I can work on this, however some initial hand-holding might be needed. I will join tomorrow's community call to see if this will work.

    Reacted by Erik Welch
  5. eriknw commented on Nov 1, 2022

    @eriknw
    eriknw
    on Nov 1, 2022
    MemberAuthor
    More actions

    Thanks @SultanOrazbayev, I'm delighted to hear this and look forward to seeing you tomorrow!

    We'll be happy to hand-hold to get you started. I've been trying (and failing) to work on the contributor guidelines document for a few months now (my bad!), so the weekly call and joining us on discord and/or slack for async chatting are the best ways to get started and ask questions.

    Discord (Community Packages / #graphblas): https://discord.com/invite/vur45CbwMz
    Slack (#python-bindings channel): https://join.slack.com/t/thegraphblas/shared_invite/zt-1iatqh6eq-39HFogw1U8kKRKo2YQOFzQ

    I prefer Discord and would like to move away from Slack, but I suspect we'll be at both for a while.

  6. eriknw commented on Nov 3, 2022

    @eriknw
    eriknw
    on Nov 3, 2022
    MemberAuthor
    More actions

    Alright, I think the best way to begin adding type annotations is very gradually with generics (and without running mypy).

    For example, we can make types such as:

    from typing import TypeVar
    
    ScalarLike = TypeVar("ScalarLike")  # Scalar, ScalarExpression, int, float, list, dict, etc.
    VectorLike = TypeVar("VectorLike")  # Vector, VectorExpression, etc.
    MatrixLike = TypeVar("MatrixLike")  # Matrix, TransposedMatrix, MatrixExpression, etc.

    and use these as input types.

    As @jim22k suggested today, it's probably best to begin with user-facing objects Scalar, Vector, and Matrix. We should be able to handle most return types such as VectorExpression. In core/vector.py, we may need to define MatrixExpression as a generic type for use with Vector.outer. For now, inline type annotations are for readers, not for type checkers.

    To make type checkers happy, I think we'll want to add .pyi stub files that can be exact and use the correct types. Mypy is able to generate stubs via stubgen graphblas. I would wait to do this though. The mypy documentation appears pretty good at a glance.

    I think the eventual target is to run checks with mypy, maybe pyright (to make sure VSCode is happy), and maybe pydantic. It'll be non-trivial to get there, but I think it would improve the code base.

    Reacted by Sultan Orazbayev
  7. MridulS commented on Nov 3, 2022

    @MridulS
    MridulS
    on Nov 3, 2022
    More actions

    and without running mypy

    Any specific reason for this? Would it be better to first add mypy to the CI? We can still "typecheck" without having any types.

  8. eriknw commented on Nov 3, 2022

    @eriknw
    eriknw
    on Nov 3, 2022
    MemberAuthor
    More actions

    @MridulS, I didn't know you know about type-checking--this is great!

    Yeah, if we can figure out how to run mypy even with no type annotations, then we probably should.

    My naive attempt, mypy graphblas, showed a lot of errors, but I'm new to mypy and am probably doing things wrong.

  9. SultanOrazbayev commented on Nov 7, 2022

    @SultanOrazbayev
    SultanOrazbayev
    on Nov 7, 2022
    Member
    More actions

    Checking the docs on using mypy with an existing codebase and experimenting a bit locally with mypy, there are at least a couple of options:

    1. Setup a CI so that it checks only a specific file where the type annotation work will be done first and then gradually expand the range of files. Roughly:
    mypy --ignore-missing-imports --follow-imports=skip graphblas/core/matrix.py

    This will currently run without any errors (without any type definitions), so should be a good test for checking validity of introduced type hints.

    1. Setup a CI to check all files, but suppress the non-critical errors via setup a mypy.ini with the following contents:
    [mypy]
    ignore_missing_imports = True
    ignore_errors = True

    I think first option is a bit more explicit in terms of coverage, so if I start working on https://github.com/python-graphblas/python-graphblas/blob/main/graphblas/core/matrix.py that could be the first file to check.

  10. eriknw commented on Nov 7, 2022

    @eriknw
    eriknw
    on Nov 7, 2022
    MemberAuthor
    More actions

    Very nice!

    This shows how much I know, which isn't much :-P. I'm happy to follow what you think is best here @SultanOrazbayev. I think I'm going to learn a lot from this effort.

    Do you think we'll ever need to "cheat" by e.g. adding graphblas/core/typing.py like so:

    from typing import TypeVar, Union
    
    Matrix = TypeVar("Matrix")
    TransposedMatrix = TypeVar("TransposedMatrix")
    MatrixInfixExpr = TypeVar("MatrixInfixExpr")
    MatrixExpression = TypeVar("MatrixExpression")
    MatrixIndexExpr = TypeVar("MatrixIndexExpr")
    # MatrixLike can be used as function input argument type
    MatrixLike = Union[Matrix, TransposedMatrix, MatrixInfixExpr, MatrixExpression, MatrixIndexExpr]
    Reacted by Sultan Orazbayev
  11. SultanOrazbayev commented on Nov 8, 2022

    @SultanOrazbayev
    SultanOrazbayev
    on Nov 8, 2022
    Member
    More actions

    Not sure if I understand correctly, but if you are suggesting that we collect all the typing definitions in one file, that makes a lot of sense. I started placing typing definitions in the same file, but realize this won't work as the same definitions will need to be reused in other files.

    In terms on naming, will the suggested path graphblas/core/typing.py cause import conflicts due to naming? e.g. within graphblas/core/*py scripts, when I run from typing import TypeVar, isn't it going to use graphblas/core/typing.py (instead of the built-in typing)?

  12. eriknw commented on Nov 8, 2022

    @eriknw
    eriknw
    on Nov 8, 2022
    MemberAuthor
    More actions

    I think we should try to use the actual types in type annotations. I worry that this will be difficult or virtually impossible due to how interdependent everything is and how limited forward declaration of types may be.

    When using the real type is not feasible, I think it's reasonable to use a fake type (using TypeVar) of the same name instead. Again, I'm not an expert here, and I wouldn't be surprised if mypy can work around my concerns. But, in the spirit of "gradual typing", I'm okay with adding type annotations even if some are only for humans and currently worthless for type validation tools. That is, I would defer "let's figure out how to use actual types" until later if it's really hard. Maybe we'll need to use stub files later on.

    Since Python 3, import typing and from typing import TypeVar will always refer to the builtin module (i.e., it's absolute, not relative), and e.g. from .typing import foo is always relative in the current package.

    Reacted by Sultan Orazbayev
  13. eriknw commented on Mar 30, 2023

    @eriknw
    eriknw
    on Mar 30, 2023
    MemberAuthor
    More actions

    The discussions in these links seem informative and helpful:

    • Annotations guidelines dask/community#255
    • Support type annotations (PEP 484) numpy/numpydoc#196
    Reacted by Sultan Orazbayev
  14. eriknw commented on Apr 6, 2023

    @eriknw
    eriknw
    on Apr 6, 2023
    MemberAuthor
    More actions

    MonkeyType is an interesting and potentially useful tool for creating stub files and adding annotations:
    https://github.com/Instagram/MonkeyType

    Example usage:

    $ monkeytype run -m pytest
    $ monkeytype stub graphblas.core.matrix
    

    It still needs a human touch, but it appears to be a useful tool to get things started. I don't know if it can create "modern" annotations such as not using Optional, Union, or List, but there's proabably a way to do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussionDiscussing a topic with no specific actions yetDiscussing a topic with no specific actions yetgood first issueClearly described and easy to accomplish. Good for beginners to the project.Clearly described and easy to accomplish. Good for beginners to the project.hygieneImprove code quality and reduce maintenance overheadImprove code quality and reduce maintenance overheadlowpriority

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.