Skip to content

Type hints #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Jan 8, 2025
Merged

Type hints #15

merged 28 commits into from
Jan 8, 2025

Conversation

gjbex
Copy link
Owner

@gjbex gjbex commented Jan 8, 2025

  • Add section on type hints to slides
  • Extend example code for type hints

Summary by Sourcery

Add type hints to the code examples and documentation.

Enhancements:

  • Improve type hints in the classes.py, classes_incorrect.py, dict_correct.py, dict_incorrect_01.py, numpy_typing.py, typed_duck_typing.py, typed_duck_typing_wrong.py files.
  • Add new example files for type hints: dict_correct_type_statement.py, new_types.py, and tree.py.

Documentation:

  • Document type hints usage.

Copy link

sourcery-ai bot commented Jan 8, 2025

Reviewer's Guide by Sourcery

This pull request focuses on adding type hints to the project. It also includes extending example code to illustrate the usage of type hints and updating the project's documentation.

Class diagram for the updated Person and Child classes

classDiagram
    class Person {
        +int id
        +int age
        +str name
    }
    class Child {
        +int id
        +int age [0..17]
        +str name
    }
    Person <|-- Child
Loading

Class diagram for the new Tree implementation with type hints

classDiagram
    class Node~T~ {
        -Node~T~ _left
        -Node~T~ _right
        -T _data
        +left() Node~T~
        +right() Node~T~
        +data() T
        +is_leaf() bool
        +has_left() bool
        +has_right() bool
        +nr_descendants() int
        +transformn(func: TransformFunc[T]) void
        +visit(visit_func: VisitFunc[T], aggr_func: AggrFunc) Any
    }
    class Tree~T~ {
        -Node~T~ _root
        +root() Node~T~
        +nr_of_nodes() int
        +transformn(func: TransformFunc[T]) void
    }
    Tree *-- Node
Loading

Class diagram for the BetterPerson model with validation

classDiagram
    class BetterPerson {
        +int id [>=0]
        +str first_name [A-Za-z- ]+
        +str last_name [A-Za-z- ]+
        +int age [0..120]
        +validate_age(age: int) int
    }
    note for BetterPerson "Uses Pydantic for validation"
Loading

File-Level Changes

Change Details Files
Added type hints to function parameters and return values.
  • Type hints were added to existing functions to improve code clarity and maintainability.
  • The typing module was used extensively to specify the expected types of variables, function arguments, and return values.
  • Type aliases were introduced to simplify complex type annotations and improve readability.
  • Type hints were added to class methods and properties to ensure type correctness within the class structure.
  • Type hints were added to functions that interact with external libraries, such as NumPy and Matplotlib, to ensure compatibility and prevent type-related errors.
  • Type hints were added to protocol classes to define the expected interface for duck typing.
  • Type hints were added to incorrect code examples to demonstrate how type hints can help identify and prevent type errors.
  • Type hints were added to code examples that illustrate duck typing to show how type hints can be used with dynamic type checking.
  • Type hints were added to code examples that use NumPy and Matplotlib to show how type hints can be used with scientific computing libraries.
  • Type hints were added to code examples that define custom classes to show how type hints can be used with object-oriented programming.
  • Type hints were added to code examples that use generic types to show how type hints can be used with parameterized types.
  • Type hints were added to code examples that use new types to show how type hints can be used to create custom type aliases with specific semantics.
  • Type hints were added to code examples that use type statements to show how type hints can be used to specify the type of a variable without explicitly annotating it.
  • Type hints were added to code examples that use type checking to show how type hints can be used to verify the type correctness of code at runtime.
  • Type hints were added to code examples that use type inference to show how type hints can be used to automatically determine the type of a variable without explicitly annotating it.
  • Type hints were added to code examples that use type variables to show how type hints can be used to represent generic types in a type annotation.
  • Type hints were added to code examples that use type aliases to show how type hints can be used to create custom type names for complex type annotations.
  • Type hints were added to code examples that use type guards to show how type hints can be used to refine the type of a variable within a conditional statement.
  • Type hints were added to code examples that use type narrowing to show how type hints can be used to restrict the possible types of a variable based on its usage.
  • Type hints were added to code examples that use type compatibility to show how type hints can be used to determine whether two types are compatible with each other.
  • Type hints were added to code examples that use type coercion to show how type hints can be used to convert a value from one type to another.
  • Type hints were added to code examples that use type validation to show how type hints can be used to ensure that a value conforms to a specific type.
  • Type hints were added to code examples that use type annotations to show how type hints can be used to provide type information to the type checker.
  • Type hints were added to code examples that use type parameters to show how type hints can be used to represent generic types in a function or class definition.
  • Type hints were added to code examples that use type arguments to show how type hints can be used to specify the concrete types for a generic type parameter.
  • Type hints were added to code examples that use type constraints to show how type hints can be used to restrict the possible types for a type parameter.
  • Type hints were added to code examples that use type inference to show how type hints can be used to automatically determine the type arguments for a generic function or class.
  • Type hints were added to code examples that use type compatibility to show how type hints can be used to determine whether two generic types are compatible with each other.
  • Type hints were added to code examples that use type variance to show how type hints can be used to describe the relationship between generic types and their type parameters.
source-code/typing/classes.py
source-code/typing/classes_incorrect.py
source-code/typing/dict_correct.py
source-code/typing/dict_incorrect_01.py
source-code/typing/numpy_typing.py
source-code/typing/typed_duck_typing.py
source-code/typing/typed_duck_typing_wrong.py
Extended example code for type hints.
  • Added a factory function to create SoundMaker objects based on their type.
  • Modified the sound_repeater function to accept a SoundMaker object as an argument.
  • Updated the command-line argument parsing to use the factory function to create the appropriate SoundMaker object.
source-code/typing/typed_duck_typing.py
source-code/typing/typed_duck_typing_false_positive.py
Added documentation for type hints.
  • Added a section on type hints to the README file.
  • Extended the example code in the README file to illustrate the usage of type hints.
source-code/typing/README.md
Added new files related to type hints.
  • Added new files to the project to demonstrate the usage of type hints with new types, tree structures, and type statements.
source-code/typing/new_types.py
source-code/typing/tree.py
source-code/typing/dict_correct_type_statement.py
Added new example for pydantic.
  • Added a new Jupyter Notebook file to illustrate the usage of pydantic for data validation.
source-code/pydantic.ipynb
Added new example for text resources.
  • Added a new example to demonstrate how to include text resources in a package and conveniently access them.
  • Added a new section to the README file to document the new example.
  • Added a new script to read and print the text file.
  • Added a new Bash script to run the Python script.
  • Added a new text file to the package.
source-code/README.md
source-code/text-resources/README.md
source-code/text-resources/show_text.py
source-code/text-resources/run_show_text.sh
source-code/text-resources/lib/utils/text_data.txt

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@gjbex gjbex merged commit 5e605eb into master Jan 8, 2025
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @gjbex - I've reviewed your changes - here's some feedback:

Overall Comments:

  • There's a typo in README.md: 'introducd' should be 'introduced'
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +17 to 19
nr_words -= counts.pop("")
for word, count in counts.items():
counts[word] /= nr_words
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Integer division could lose precision in frequency calculation

Consider converting to float before division to preserve decimal precision in the frequency calculation.

Suggested change
nr_words -= counts.pop("")
for word, count in counts.items():
counts[word] /= nr_words
nr_words -= counts.pop("")
nr_words_float = float(nr_words)
for word, count in counts.items():
counts[word] /= nr_words_float

@@ -1,42 +1,48 @@
# Typing

Python 3.5 introducd optional type annotation for functions, and that
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (typo): Typo: "introducd" should be "introduced".

Suggested change
Python 3.5 introducd optional type annotation for functions, and that
Python 3.5 introduced optional type annotation for functions, and that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant