There's nothing wrong with planning ahead, noone would blame you for not finishing your degree before applying.
However if you want them to take you seriously, and consider you for a position they might have, you kinda need to be not too far away from a speculative starting day. For that reason I'd say more than 6 months before graduation might be not optimal.
Also consider that in some countries, Sweden being one, PhD positions are full-time employment positions and they are announced publicly (even if a candidate is already present). From an employer's (or PI's) point-of-view, they need to show that they have the resources for the full-time employment, and that it's much difficult to "get rid of" someone, if the working dynamics aren't optimal, once a candidate is recruited.
What that means is that there are usually short-term, project assistant/worker positions that are meant to be "testing the water" for both parties (mine was about 8-9 months). It could also be so that the PI is waiting to hear from a funding agency for funds, and doesn't have the liberty to recruit immediately.
Such a "test period" is useful for the PI to check whether or not a candidate has what the job/project demands (academically and personally), and for the candidate it's a great chance to see the group dynamics, have a feel for how it is to work with that PI and his/her group, to work on that particular project.
So if the PI you contact does not immediately "accept" you for a position but instead offers a short-term position with the possibility of a full-term commitment, don't get dismayed :)