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

Can't type annotate deap.creator.Individual #480

Open
markf94 opened this issue Apr 26, 2020 · 2 comments
Open

Can't type annotate deap.creator.Individual #480

markf94 opened this issue Apr 26, 2020 · 2 comments

Comments

@markf94
Copy link

markf94 commented Apr 26, 2020

Unfortunately, I cannot annotate any arguments of type deap.creator.Individual when instantiating the Individual as part of a method on some class since the deap.creator.Individual class doesn't exist when the Python type checker runs.

Example:

from deap import base, creator


class Algo:
    """
    Some evolutionary algorithm.
    """

    @staticmethod
    def mate(individual: creator.Individual):
        """
        Method to mate individuals.
        """

        pass

    def setup(self):
        """
        Method that configures deap.
        """

        creator.create("FitnessMulti", base.Fitness, weights=(-1.0, -1.0))
        creator.create("Individual", list, fitness=creator.FitnessMulti)

    def optimize(self):
        """
        Method that runs the algorithm.
        """
        pass

I'm aware that deap is more script rather than OOP based but I'm curious if other people have encountered this problem and what their workarounds are.

This issue relates somewhat to #367 which is about type annotating the deap source code.

@fmder
Copy link
Member

fmder commented Apr 26, 2020

Unfortunately you are right, we are working on a new schema for the individuals, but it is not ready yet. In the mean time you could use string style annotation.

from deap import creator

class Algo:
    @staticmethod
    def mate(individual: "creator.Individual"):
        pass

@markf94
Copy link
Author

markf94 commented Apr 26, 2020

Interesting. I did not know about string style annotation. Will give it a try and report back!

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

No branches or pull requests

2 participants