Skip to content
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

Ignore missing annotation in __init__ method by algorithms-keeper #7968

Open
Cjkjvfnby opened this issue Nov 6, 2022 · 3 comments
Open

Ignore missing annotation in __init__ method by algorithms-keeper #7968

Cjkjvfnby opened this issue Nov 6, 2022 · 3 comments
Labels
enhancement This PR modified some existing files

Comments

@Cjkjvfnby
Copy link
Contributor

Feature description

Please provide return type hint for the function: init. If the function does not return a value, please provide the type hint as: def function() -> None:

Adding -> None have not sense, since this is a special method and it always returns None. Aslo this creates noise in code and even decreases readability when it's the reason for extra line breaks.

For example, this line fits without annotation and is split into 3 lines with it.

-    def __init__(self, initial_block_size: int = 8, capacity_factor: float = 0.75) -> None:
+    def __init__(
+        self, initial_block_size: int = 8, capacity_factor: float = 0.75
+    ) -> None:
@Cjkjvfnby Cjkjvfnby added the enhancement This PR modified some existing files label Nov 6, 2022
@Preetiraj3697
Copy link

!assign

@Preetiraj3697
Copy link

The init method is a special method in Python used to initialize objects of a class. It does not return a value and should not have a return type hint. However, the method signature can have type hints for the parameters it accepts.

Here's an example:

class MyClass:
    def __init__(self, arg1: str, arg2: int = 42) -> None:
        self.arg1 = arg1
        self.arg2 = arg2

In this example, the init method takes two arguments, arg1 which is of type str, and arg2 which is of type int. The method does not return anything and is annotated with -> None. However, this annotation is not necessary and can be omitted.

@amirsoroush
Copy link
Contributor

Just to mention, PEP 484 stats that __init__ should be annotated with -> None:

(Note that the return type of __init__ ought to be annotated with -> None. The reason for this is subtle. If __init__ assumed a return annotation of -> None, would that mean that an argument-less, un-annotated __init__ method should still be type-checked? Rather than leaving this ambiguous or introducing an exception to the exception, we simply say that __init__ ought to have a return annotation; the default behavior is thus the same as for other methods.)

@Cjkjvfnby For the mentioned case, you can put a comma after the last parameter and Black puts every parameter on a single line like:

def __init__(
    self,
    initial_block_size: int = 8,
    capacity_factor: float = 0.75,
) -> None:
    pass

Did I make it worse?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This PR modified some existing files
Projects
None yet
Development

No branches or pull requests

3 participants