The last time Paul Feig (creator of “Freaks and Geeks”) wanted to direct a female-driven comedy, the studios told him no one would go see it. Comedies, the all-male-fronted studios told him, were a man’s game. He made it anyway, with a script from Kristen Wiig, and the movie, “Bridesmaids,” went on to make $288 million and pick up two Oscar nominations. Cue a hundred articles about how women can be funny. Cue most of America wondering why anyone was writing articles about something they already knew. Feig is back with “The Heat” and he’s doubling down on the whole “female” thing. The bet pays off. “The Heat” subverts the typical traps of buddy-cop comedies, delivering a fun and filthy mix of laughs and substance. Eat your heart out, Hollywood.

The last comedy Sandra Bullock starred in was 2009’s “All About Steve,” which currently sits at seven percent on Rotten Tomatoes. So the choice to cast her opposite Melissa McCarthy, a weapon of mass hilarity, was a bold one. It works. Bullock plays Special Agent Sarah Ashburn, a dorky, socially awkward know-it-all assigned to track down a Boston drug dealer. To her surprise, no one appreciates how much of a douche she is, and her boss tells her to nail this case and make nice with her co-workers if she wants a shot at a just-available promotion. Of course, she immediately butts heads with local detective Shannon Mullins (Melissa McCarthy in all her ball-busting glory). The plot rolls on in typical fashion, but is entertaining enough to make me want to know what happens next. Despite a basic plot, The Heat shines as it explores the relationship between Mullins and Ashburn.

Fieg, it seems, let McCarthy, as Mullins, go bananas, simply aiming her giant comedic laser this way or that. Regardless of what happened on set, it feels improvised, and it brings a loose energy that lets Bullock, as an actress, as well as her character Ashburn, break out of her shell. In almost every scene, Mullins is continuously letting fly a stream of profanity. In fact, most of the jokes center around Mullins insulting either Ashburn or another character they run into. Seriously, this is the most “fucks” I’ve heard in a movie that wasn’t made by Brazzers. It might not sound interesting, but McCarthy is so filthy and funny, she elevates gags that were only OK on paper. The physical comedy too, is next level hilarious, and Feig, who directed some of the best “Office” episodes, knows exactly how to capture it. I do wish, this being an action movie, that there was more action. While the interrogation scenes were sometimes tense and often funny, the action often lacked punch. There are a few tonal problems, too, especially when transitioning from the comedy to more serious elements.

What makes “The Heat” work beyond just a buddy-cop comedy is the meta-commentary it offers on women in the workplace in general, and women in comedies specifically. Ashburn and Mullins tear each other apart in a way that is often associated with women. And yet, about a third of the way into the movie, a male character throws in all the common stereotypes about why women can’t be in positions of power — too emotional, too weak — and, after that, Mullins and Ashburn are united. The fights their characters had, from that point on, were more over actions and behaviors instead of personality differences. It was subtle, but it shifted the dynamic to one that pitted the two women against everyone else — their bosses, the co-workers, their enemies — all male. Beyond that, there was a definite through line about those Hollywood execs that don’t believe in female-led comedies. Despite the fact that Mullins and Ashburn are the best at their respective jobs, everyone doubts them. They are forced to prove that they have bigger balls than any of the males they report to, despite having a killer track record and years of experience.

While not as tight as “Bridesmaids,” “The Heat” brings a huge bowl of funny with enough emotional resonance to last beyond the first viewing. Clearly, women are here to stay. As if I, or anyone else, needed to fucking tell you that. “The Heat” offers one of the summer’s best surprises– a fresh, sophisticated buddy comedy broad enough to appeal to a wide audience and smart enough to please the comedy nerds.