DEAP / deap

Distributed Evolutionary Algorithms in Python
http://deap.readthedocs.org/
GNU Lesser General Public License v3.0
5.82k stars 1.13k forks source link

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

Open markf94 opened 4 years ago

markf94 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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