Skip to content

Support Type Hints #2794

Open
Open

Description

@seisman

This issue is still in draft mode. Type hints are something new to me, so I need to learn more and will keep improving the issue description.

What's Python's Type Hints

Why we should support type hints

Copied from https://vegibit.com/introduction-to-python-type-hints/.

  • Improved code readability: Type hints make it easier for other developers to understand your code. By clearly specifying the types of arguments and return values, you can help others quickly grasp the intent of your code.
  • Improved development speed: With type hints, you can catch potential bugs and errors early in the development process. This can save you time in the long run, as you can fix errors before they cause problems in your code.
  • Improved tooling support: Many tools, such as static analyzers and IDEs, use type hints to provide better support for your code. For example, they can use type hints to catch potential errors, suggest code improvements, or provide better autocompletion.
  • Better auto-completion for parameter values: See POC: Showcases for support type hints #2793 for how Type Hints can affect the way we use PyGMT.

Tasks

References:

Related PEPs

Activity

  1. added
    question
    feature request
    longterm
    on Nov 2, 2023
  2. weiji14 commented on Nov 11, 2023

    @weiji14
    Member

    Personally I do use type hints (and tried to add it to pygmt.grdtrack in 2019 apparently - #308 (comment)), but it can be a lot of work to add them across the whole project (and a bit tough for new contributors to learn).

    Maybe we can start small with just typing.Literal since that's a practical one for when there's a choice of str type arguments, e.g. load_earth_relief(..., data_source: Literal["igpp", "gebco", "gebcosi", "synbath"], ...) in #2793? Other types like bool/int/float don't offer any autocomplete so may be less useful for now.

  3. seisman commented on Nov 13, 2023

    @seisman
    MemberAuthor

    Yes, it's definitely a lot of work and a long-term goal. We need to split it into multiple smaller jobs so that people can help.

  4. willschlitzer commented on Nov 15, 2023

    @willschlitzer
    Contributor

    Maybe we can start small with just typing.Literal since that's a practical one for when there's a choice of str type arguments, e.g. load_earth_relief(..., data_source: Literal["igpp", "gebco", "gebcosi", "synbath"], ...) in #2793? Other types like bool/int/float don't offer any autocomplete so may be less useful for now.

    I think specifying literal argument options is a good start.

  5. added
    typing
    and removed
    question
    feature request
    on Nov 15, 2023
  6. seisman commented on Oct 24, 2025

    @seisman
    MemberAuthor

    What are the best type hints for parameters that expect a fixed number of values?

    In GMT, many parameters accept a sequence with a fixed number of elements. For example, the panel parameter can take a sequence of two values, such as panel=(1, 2).

    There are two common options for type hints:

    1. tuple[int, int]: It clearly indicates that the parameter expects exactly two integers. However, it restricts the argument to a tuple, excluding lists or NumPy arrays.
    2. Sequence[int]: It accepts any sequence-like object (e.g., tuple, list, array), but does not explicitly specify the expected number of elements. [We're doing the checking at run time in the AliasSystem class.]

    Currently, both styles are used, but I prefer option 2 for flexibility and plan to update the code for consistency.

    @GenericMappingTools/pygmt-maintainers Comments?

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

    longtermtyping

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions