The only way Gus Van Sant’s “Milk,” a biopic of America’s first openly gay public official, could have been more timely is if it had been released before the election, not after. The parallels between Harvey Milk’s crusading against anti-gay Proposition 6 in 1978 and the campaign against anti-gay-marriage Proposition 8 in 2008 are uncanny. The only difference is the outcome — but who knows, Prop 8 might have gone differently if “Milk” had come out in time to earn more voters’ sympathy.

Make no mistake: If you believe the mainstreaming of homosexuality is wrong, then “Milk” is a dangerous film. In its depiction of the problems gays faced in San Francisco in the 1970s, including police harassment and blatant civil-rights violations, it shows how much things have changed while subtly reminding us of how they haven’t. It would be hard to watch it and not come away with more sympathy for the gay community and indignation over the injustices it has faced. And that makes the film either an inspiring biography or a shrewd chunk of propaganda, depending on your views.

Sean Penn, in what might be the most likable performance of his self-serious career, plays Harvey Milk, a gay New Yorker who moved to San Francisco in the early 1970s, when he was in his early 40s. He’s accompanied by his younger boyfriend, Scott (James Franco), and soon finds that he wants to do something useful with his life. He sets up a camera shop on Castro Street, at the center of what is starting to be San Francisco’s gay community, then fights back against the anti-gay business owners nearby who harass him.

Harvey quickly becomes a local activist and organizer, the “mayor of Castro Street” (“I may have invented the term myself,” he confides), making inroads even with traditionally heterosexual groups like truck drivers and labor unions. He persuades people of the power of the gay voting bloc. Gays are not to be marginalized and disregarded anymore, not on Harvey’s watch. Meanwhile, at home, the apolitical Scott just wants his boyfriend back.

Harvey’s dedication to public life only increases, though, as he starts running for office and becomes surrounded by campaign managers, strategists, volunteers, and activists. After a few failed tries he’s elected to the San Francisco Board of Supervisors in 1977, thus becoming, in the words of Time magazine, “the first openly gay man elected to any substantial political office in the history of the planet.”

The historical significance of this is not lost on Harvey’s detractors. These include the shrill harridan Anita Bryant (seen in archival footage) and State Sen. John Briggs (Denis O’Hare) — the man behind Proposition 6, which would make it legal for schools to fire employees solely for being gay. Briggs’ ignorance of homosexuality (he seems to think it’s contagious) would be hilarious if it weren’t so alarmingly common in the 1970s and not totally unknown today. (A nice touch: The actor who plays Briggs is openly gay, a fact that would surely horrify the real Briggs, which is probably why Van Sant cast him.)

The film is punctuated by snippets of Harvey alone in his apartment in 1978, recording a tape to be played in the event of his assassination. He is well aware of the dangers of his notoriety, and he faces it even among his fellow board members, including Dan White (Josh Brolin), a family man who seems to desire Harvey’s friendship as much as his downfall.

Penn’s performance is key, of course. The best testimonial I can give is to point out how much I dislike his dour, humorless real-life persona, then weigh that against how much I love his character in “Milk.” Some actors excel at playing people who aren’t much different from themselves; Penn, it seems, is playing a man who is his polar opposite — outgoing, charismatic, optimistic, and smiling — and doing a fantastic job of it. Acting, ladies and gentlemen! Acting!

Speaking of acting, there are some good examples of it in the other roles, too. James Franco’s sweet, understated turn as Harvey’s longtime partner is quietly effective; Diego Luna shines in his few scenes as one of Harvey’s slightly less stable lovers; and Josh Brolin, who just played another in-over-his-head politician in “W.,” actually earns some empathy as Harvey’s nemesis.

Van Sant, working from a screenplay by gay ex-Mormon Dustin Lance Black, has constructed a flawless re-creation of the sights and sounds of the 1970s. It’s nearly impossible to tell where the archival footage stops and the re-enactments begin. After a series of obtuse, difficult movies, it’s great to see Van Sant making something that’s accessible — entertaining, emotional, and vibrant — without forsaking depth and intelligence. “Milk” hits the usual biopic notes, including the career-focused protagonist whose ambition causes him to neglect his spouse or partner, but does it without indulging in too much cliché or formula. This middle ground between the avant-garde weirdness of “Last Days” and the total commercial sell-outism of “Finding Forrester” is a good place for Van Sant. I hope he stays here for a while.

B+ (2 hrs., 8 min.; R, some harsh profanity, a little nudity, brief strong sexuality, brief violence.)